I'm trying to group my multiIndex as
df.groupby(level=1, 'Amount($m)').sum()
But I'm getting the error that level > 0 is only valid with a MultiIndex. When I call df.info() I get
class 'pandas.core.frame.DataFrame'
but when I print df.columns
I get
MultiIndex(levels=[[u'MSS', u'MLLN', u'AMS'],
[u'Trades', u'Resolved Trades'],
[u'Amount($m)', u'Trades', u'Resolved Trades']],
labels=[[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], [1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0]],
names=[u'Entity', None, None])
Can someone explain what I'm missing?
Pandas dataframes have two index objects:
The default for the
groupby
method is to group by the values in a column or columns (levels of the column index). You however attempted to group by the values in one of the levels of the row index. Your row index has only 1 level (level 0) and this is why you received an error.It looks like you are wanting to group in the other direction by the column names themselves in the bottom level of the column MultiIndex object. Try changing the axis parameter to 1 and choose level 2. This might not be what you are looking for. Supplying more data would help.
Here is a contrived example similar to what you see in the the documentation.
Then run the first statement I wrote:
Output
Finally, you can select just
Amount($m)