maracuja
maracuja

Reputation: 447

Group dataframe on column values whilst summing over another column

I have a dataframe like below

   A     B   C   D         
0  cat   5   aa  X
1  wolf  8   bb  Y
2  dog   3   cc  Z
3  cat   6   aa  X
3  dog   6   cc  Z

I want to group by A and sum over C. I know I can do the following

>>>df.groupby('A').sum().C

A     C
cat   7 
wolf  1
dog   9

but it returns a series and I want all the static data for each value of A to get something like

   A     B   C   D         
0  cat  11   aa  X
1  wolf  1   bb  Y
2  dog   9   cc  Z

Any ideas?

Upvotes: 1

Views: 47

Answers (1)

EdChum
EdChum

Reputation: 393873

You could use transform to overwrite column 'B' in original df and then call drop_duplicates:

In [95]:
df['B'] = df.groupby('A')['B'].transform('sum')
df = df.drop_duplicates('A')
df

Out[95]:
      A   B   C  D
0   cat  11  aa  X
1  wolf   8  bb  Y
2   dog   9  cc  Z

Upvotes: 1

Related Questions