0

hello I wanted to ask a similar question answered here: Pandas group-by and sum

I could not comment my question in that link as i had less than 50 reputation.

That's why I am asking here: I wanted to achieve the same result as the one asked in the link above. But in my case I want to get the sum of last n columns after group by.

I have something like this:

A B C D E F G H I J K L
0 2 5 4 3 2 1 8 5 6 3 3
0 2 8 6 5 2 4 6 2 4 5 2 
0 2 5 6 4 5 6 6 6 6 6 8 
1 5 6 5 6 5 6 5 6 8 5 2 
1 5 7 6 5 4 6 3 8 5 2 2
1 5 4 5 6 3 2 1 4 5 6 9

This is what I tried:

df.groupby(['A', 'B'])[['C','D','E','F','G','H','I','J']].agg('sum')

But I suppose it is not possible when there is close to 50 columns Is there any way I can select the last n number of columns after groupby and take sum?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sabbir Talukdar
  • 115
  • 2
  • 11
  • 4
    can you try with `df.groupby(['A', 'B'])[df.columns.difference(['A', 'B'])].agg('sum')` ? – anky May 02 '20 at 15:48
  • https://pandas.pydata.org/docs/user_guide/indexing.html – wwii May 02 '20 at 15:58
  • It worked thanks, I have another question though, what if i have columns that I dont wish to add, say the column C, I want to group by A,B and sum all the columns except C – Sabbir Talukdar May 02 '20 at 16:00

0 Answers0