【发布时间】:2020-08-15 02:42:09
【问题描述】:
所以,我正在尝试做类似的事情:
select a, b, c, sum(d), sum(e), count(*)
from df
group by 1,2,3
换句话说,我有这个:
a b c d e
Billy Profesor 1 10 5
Billy Profesor 1 17 3
Andrew Student 8 2 7
我希望输出是:
a b c d e count
Billy Profesor 1 27 8 2
Andrew Student 8 2 7 1
我试过这个,它部分工作:
df.groupby(['a','b','c']).sum().reset_index()
我仍然无法让它为计数工作。我也在帖子Group dataframe and get sum AND count? 中尝试了答案,但是使用 agg 函数会使事情变得非常混乱,而且它会计算每一列。
更新:我更改了 c 列,因为我有一个要分组的数字列,但没有求和。
【问题讨论】: