pandas实现sum()over()、count()over()等窗口函数

pandas利用transform实现窗口函数

array =  [
    ['a', 2, 29],
    ['a', 1, 18],
    ['a', 0, 18],
    ['b', 2, 25],
    ['b', 4, 12],
    ['c', 6, 21],
]
data = pd.DataFrame(array, columns=["name", "class", "score"])
# 去重计数:nunique
data['nunique'] = data.groupby('name')['name'].transform('nunique')

# 求和:sum
data['sum'] = data.groupby('name')['score'].('sum')

# 均值:mean
data['mean'] = data.groupby('name')['score'].transform('mean')

# 其余统计函数类似

你可能感兴趣的:(pandas)