小编jAg*_*ses的帖子

Most pythonic way to sum rows in a dataframe based on index groups

I have the following pandas dataframe:

df = pd.DataFrame([[1,2,3,'a'],[4,5,6,'a'],[2,4,1,'a'],[2,4,1,'b'],[4,9,6,'b'],[2,4,1,'b']], index=[0,1,2,0,1,2], columns=['aa','bb','cc','cat'])


     aa    bb    cc    cat
0    1      2     3    a
1    4      5     6    a
2    2      4     1    a
0    2      4     1    b
1    4      9     6    b
2    2      4     1    b
Run Code Online (Sandbox Code Playgroud)

I need to add rows with the same index.

    aa   bb   cc  cat
0   3    6    4    ab
1   8   14   12    ab
2   4    8    2    ab
Run Code Online (Sandbox Code Playgroud)

I used the following code:

df_ab = …
Run Code Online (Sandbox Code Playgroud)

python pandas

2
推荐指数
1
解决办法
48
查看次数

标签 统计

pandas ×1

python ×1