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