[英]Count and Concatenate Integer in Pandas GroupBy
Let's say this is my df 让我们说这是我的df
A B C
0 a 33 13
1 b 44 14
2 a 55 15
3 a 66 16
4 b 77 17
5 c 88 18
and I try to get something like this 我试着得到这样的东西
A B B C
count list sum
0 a 3 33,55,66 44
1 b 2 44,77 31
2 c 1 88 81
Is there any pythonic way to do it? 有没有pythonic方式来做到这一点?
This is my code but it is not pythonic 这是我的代码,但它不是pythonic
df.groupby('A').agg({'B': ["count", lambda x: ','.join(x.astype(str))], 'C':sum})
You can pass a dict to agg: 你可以将一个字典传递给agg:
In [11]: df.groupby("A").agg({"B": ["count", list], "C": ["sum"]})
Out[11]:
B C
count list sum
A
a 3 [33, 55, 66] 44
b 2 [44, 77] 31
c 1 [88] 18
To add the comma, I'd use a function: 要添加逗号,我将使用一个函数:
In [21]: def list_(ls):
...: return ",".join(map(str, ls))
...:
In [22]: list_.__name__ = "list"
In [23]: df.groupby("A").agg({"B": ["count", list_], "C": ["sum"]})
Out[23]:
B C
count list sum
A
a 3 33,55,66 44
b 2 44,77 31
c 1 88 18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.