Chr*_*ian 5 python aggregate pandas pandas-groupby
我正在尝试groupby
在 Pandas 中使用自定义函数。我发现 usingapply
允许我通过以下方式做到这一点:
(从两组计算新平均值的示例)
import pandas as pd
def newAvg(x):
x['cm'] = x['count']*x['mean']
sCount = x['count'].sum()
sMean = x['cm'].sum()
return sMean/sCount
data = [['A', 4, 2.5], ['A', 3, 6], ['B', 4, 9.5], ['B', 3, 13]]
df = pd.DataFrame(data, columns=['pool', 'count', 'mean'])
df_gb = df.groupby(['pool']).apply(newAvg)
Run Code Online (Sandbox Code Playgroud)
是否可以将其集成到agg
函数中?沿着这些路线:
df.groupby(['pool']).agg({'count': sum, ['count', 'mean']: apply(newAvg)})
Run Code Online (Sandbox Code Playgroud)
国际大学学院
df.groupby(['pool']).apply(lambda x : pd.Series({'count':sum(x['count']),'newavg':newAvg(x)}))
Out[58]:
count newavg
pool
A 7.0 4.0
B 7.0 11.0
Run Code Online (Sandbox Code Playgroud)