I have a MultiIndex dataframe that I'm trying to index in to based on value ranges in my columns and the outermost index levels. So, using the example below, e.g. I'm trying to select the values from v2
that are index l2
where v1 > 12
I can achieve this using multiple index statements, such as:
df[df.v1>12].loc['l2', 'v2']
, but this seems less than ideal. Is there a way to condense this in to a single statement?
I've been trying to figure out how to use pd.IndexSlice
, but can't seem to wrap my head around what the examples in the MultiIndex section of the docs are doing.
df = pd.concat([pd.DataFrame({'v1': range(10, 15), 'v2':range(5, 0, -1)})
for i in range(2)], keys=['l1', 'l2'])
v1 v2
l1 0 10 5
1 11 4
2 12 3
3 13 2
4 14 1
l2 0 10 5
1 11 4
2 12 3
3 13 2
4 14 1
You can use slicers for selecting and then modified
boolean indexing
withloc
for select columnv2
:Another solution with
xs
:Solution with selecting first level of index by
get_level_values
, last if need remove first level usedroplevel
orreset_index
:Example with
IndexSlice
:Select all values in first level and in second from
1
to3
(thanks piRSquared):