The isin() method applied to a Pandas index returns whether each index value is found in the passed set of values, but is there a possibility to test only a set of indexe values ?
In the multiIndex below I would like to test if an index with level name s1 and level value E and level name d1 and level value future exists. Problem is that I test index values that may not exist so selection method like xs or loc don’ work.
df_routes.index
MultiIndex([('E', 'future', 'B', 'future'),
('E', 'future', 'B5', 'future'),
('E', 'future', 'B', 'spot'),
('B', 'future', 'B', 'future'),
('B', 'future', 'B5', 'future'),
('B', 'future', 'B', 'spot')],
names=['s1', 'd1', 's2', 'd2'])
My problem is that isin() returns False when using any as index values :
df_routes.index.isin([('E', 'future', any, any)])
array([False, False, False, False, False, False])
The desired result would be :
array([True, True, True, False, False, False])
There is a similar question here but the answer only works for index not multiindex.
How can i do that ?
Advertisement
Answer
If you wanna filter the dataframe using multi-index, just use .loc
df.loc[('E', 'future')]
Sample DataFrame
0
s1 d1 s2 d2
E future B future 1
B5 future 2
B spot 3
B future B future 4
B5 future 5
B spot 6
Ouput
0 s2 d2 B future 1 B5 future 2 B spot 3
To create a boolean array
(df.index.get_level_values(0) == 'E') & (df.index.get_level_values(1) == 'future')