ash
ash

Reputation: 33

Sum multiple multiindex column dataframe

I have a dataframe like

  0       1             One                       Two
Tech    Tool    First  Second  Third      First  Second  Third
Auto     UIP     23      18      5          59     56      3
 AI      ALT     45      34      11         32     12      20

Need an output like

  0       1             One                       Two                      Sum
Tech    Tool    First  Second  Third      First  Second  Third    First   Second    Third
Auto     UIP     23      18      5          59     56      3        82      74        8
 AI      ALT     45      34      11         32     12      20       77      46        31

Here for Sum First 82=23+59, Second 74=18+56, Third 8=5+3 Similarly for others. I am beginner in multiindex dataframe please help

Upvotes: 1

Views: 125

Answers (1)

MaxYarmolinsky
MaxYarmolinsky

Reputation: 1139

You can try:

df.groupby(level=1, axis=1).sum()

Upvotes: 1

Related Questions