Skip to content Skip to sidebar Skip to footer

Pandas - Select Values From A Groupby Object

I have a pandas groupby object, c: >>> c.index.names FrozenList([u'Thing1', u'Thing2', u'Month']) >>> c.columns Index([u'Tot'], dtype='object') >>> c

Solution 1:

You can just index it like other dataframes, but using tuples for the combination of levels:

c.loc[('G', 'P'), :]

However, it is possible that for such indexing operations, the multi-index needs to be sorted (otherwise it can give and exception). You can do this with:

c = c.sortlevel()

And to be correct, c is not a groupby object, but a DataFrame (you also have pandas GroupBy objects, but they are the result of a .groupby() call)

Post a Comment for "Pandas - Select Values From A Groupby Object"