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

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...