小编Jef*_*eff的帖子

Pandas GroupBy 中的计数和连接整数

假设这是我的 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
Run Code Online (Sandbox Code Playgroud)

我尝试得到这样的东西

   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
Run Code Online (Sandbox Code Playgroud)

有没有Pythonic的方法来做到这一点?

这是我的代码,但它不是 pythonic

df.groupby('A').agg({'B': ["count", lambda x: ','.join(x.astype(str))], 'C':sum})
Run Code Online (Sandbox Code Playgroud)

python concatenation pandas pandas-groupby

5
推荐指数
1
解决办法
1511
查看次数

标签 统计

concatenation ×1

pandas ×1

pandas-groupby ×1

python ×1