Filtering multiple items in a multi-index Python Panda dataframe
You can get_level_values
in conjunction with Boolean slicing.
In [50]:
print df[np.in1d(df.index.get_level_values(1), ['Lake', 'River', 'Upland'])]
Area
NSRCODE PBL_AWI
CM Lake 57124.819333
River 1603.906642
LBH Lake 258046.508310
River 44262.807900
The same idea can be expressed in many different ways, such as df[df.index.get_level_values('PBL_AWI').isin(['Lake', 'River', 'Upland'])]
Note that you have 'upland'
in your data instead of 'Upland'
Another (maybe cleaner) way might be this one:
print(df[df.index.isin(['Lake', 'River', 'Upland'], level=1)])
The parameter level
specifies the index number (starting with 0) or index name (here: level='PBL_AWI'
)