繁体   English   中英

pandas groupby聚合具有多列的自定义函数

[英]pandas groupby aggregate customised function with multiple columns

我正在尝试在pandas中使用groupby的自定义函数。 我发现使用apply允许我以下列方式执行此操作:

(从两组计算新均值的示例)

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)

是否可以将其集成到agg函数中? 沿着这些方向:

df.groupby(['pool']).agg({'count': sum, ['count', 'mean']: apply(newAvg)})

函数agg处理每个列,因此可能的解决方案是先创建列cm ,然后使用assign然后汇总sum ,最后划分每列:

df_gb = df.assign(cm=df['count']*df['mean']).groupby('pool')['cm','count'].sum()
print (df_gb)
        cm  count
pool             
A     28.0      7
B     77.0      7

out = df_gb.pop('cm') / df_gb.pop('count')
print (out)
pool
A     4.0
B    11.0
dtype: float64

带有agg的字典用于为每个系列执行单独的计算。 对于你的问题,我建议pd.concat

g = df.groupby('pool')
res = pd.concat([g['count'].sum(), g.apply(newAvg).rename('newAvg')], axis=1)

print(res)

#       count  newAvg
# pool               
# A         7     4.0
# B         7    11.0

这不是最有效的解决方案,因为您的函数newAvg正在执行可以在最初对整个数据帧执行的计算,但它确实支持任意预定义的计算。

IIUC

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

使用带有eval assign

df.assign(cm=df['count']*df['mean'])\
  .groupby('pool', as_index=False)['cm','count'].sum()\
  .eval('AggCol = cm / count')

输出:

  pool    cm  count  AggCol
0    A  28.0      7     4.0
1    B  77.0      7    11.0

如果您正在计算加权平均值,则可以使用agg和NumPy np.average函数轻松np.average 只需阅读系列中的“均值”列:

df_gb = df.groupby(['pool']).agg(lambda x: np.average(x['mean'], weights=x['count']))['mean']

您也可以使用newAvg函数执行此操作,但这会产生警告:

df_gb2 = df.groupby(['pool']).agg(newAvg)['mean']

如果您愿意使用newAvg函数,可以重新定义它以避免处理副本:

def newAvg(x):
    cm = x['count']*x['mean']
    sCount = x['count'].sum()
    sMean = cm.sum()
    return sMean/sCount

通过此修改,您可以获得预期的输出:

df_gb2 = df.groupby(['pool']).agg(newAvg)['mean']
print(df_gb2)

# pool
# A     4.0
# B    11.0
# Name: mean, dtype: float64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM