0

In Pandas, How to get mean for second level columns?

        bar                 baz                 foo          
        one       two       one       two       one       two

aaa       1         2         3         4         5         6
bbb      -1        -2        -3        -4        -5        -6

Expected:

             one       two

aaa            3         4
bbb           -3        -4
YeongHwa Jin
  • 435
  • 5
  • 15

1 Answers1

1

Use parameters level=1 and axis=1 for MultiIndex in columns:

df = df.groupby(level=1, axis=1).mean()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252