且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Pandas Groupby - 命名聚合输出列

更新时间:2023-11-21 16:31:34

根据 文档:

如果传递了 dict,则键将用于命名列.否则函数的名称(存储在函数对象中)将是用过.

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.

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

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

就你而言:

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