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
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
I used the following code:
df_ab = df[df['cat'] == 'a'] + df[df['cat'] == 'b']
But is this the most pythonic way ?