Pandas Groupby - naming aggregate output column

五迷三道 提交于 2021-02-07 19:20:38

问题


I have a pandas groupby command which looks like this:

df.groupby(['year', 'month'], as_index=False).agg({'users':sum})

Is there a way I can name the agg output something other than 'users' during the groupby command? For example, what if I wanted the sum of users to be total_users? I could rename the column after the groupby is complete, but wonder if there is another way.


回答1:


Per the docs:

If a dict is passed, the keys will be used to name the columns. Otherwise the function’s name (stored in the function object) will be used.

In [58]: grouped['D'].agg({'result1' : np.sum, ....:
'result2' : np.mean})

In your case:

df.groupby(['year', 'month'], as_index=False).users.agg({'total_users': np.sum})



回答2:


I like @Alexander answer, but there is also add_prefix:

df.groupby(['year','month']).agg({'users':sum}).add_prefix('total_')


来源:https://stackoverflow.com/questions/35076837/pandas-groupby-naming-aggregate-output-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!