微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python-Pandas Groupby-命名聚合输出列

我有一个如下的pandas groupby命令:

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

有什么办法可以在groupby命令中将agg输出命名为’users’以外的名称?例如,如果我希望用户总数为total_users怎么办?我可以在groupby完成后重命名该列,但想知道是否还有另一种方法.

解决方法:

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})

在您的情况下:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐