The latest version of Pandas supports multi-index slicers. However, one needs to know the integer location of the different levels to use them properly.
E.g. the following:
idx = pd.IndexSlice
dfmi.loc[idx[:,:,['C1','C3']],idx[:,'foo']]
assumes that we know that the third row level is the one we want to index with C1
and C3
, and that the second column level is the one we want to index with foo
.
Sometimes I know the names of the levels but not their location in the multi-index. Is there a way to use multi-index slices in this case?
For example, say that I know what slices I want to apply on each level name, e.g. as a dictionary:
'level_name_1' -> ':'
'level_name_2' -> ':'
'level_name_3' -> ['C1', 'C3']
but that I don't know the position (depth) of these levels in the multi-index. Does Pandas a built-in indexing mechanism for this?
Can I still use pd.IndexSlice
objects somehow if I know level names, but not their position?
PD: I know I could could use reset_index()
and then just work with flat columns, but I would like to avoid resetting the index (even if temporarily). I could also use query
, but query
requires index names to be compatible with Python identifiers (e.g. no spaces, etc).
The closest I have seen for the above is:
df.xs('C1', level='foo')
where foo
is the name of the level and C1
is the value of interest.
I know that xs
supports multiple keys, e.g.:
df.xs(('one', 'bar'), level=('second', 'first'), axis=1)
but it does not support slices or ranges (like pd.IndexSlice
does).