How to access MultiIndex column after groupby in pandas? How to access MultiIndex column after groupby in pandas? pandas pandas

How to access MultiIndex column after groupby in pandas?


Adding a comma after your ('c','b') tuple seems to work:

df.groupby([('c','a')])[('c','b'),].sum()

I'm guessing that without the comma, pandas is just interpreting them as separate items.


Maybe this helps explain the syntax:

df.groupby([('c','a')]).sum()         c  d             b  a   b(c, a)           2       11  4  114       15  8  15df.groupby([('c','a')])[('c','b'),('d','b')].sum()         c   d         b   b(c, a)        2       11  114       15  15