Skip to content
Advertisement

How to test a set of values (not all) exist in a Pandas multiindex?

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.

JavaScript

My problem is that isin() returns False when using any as index values :

JavaScript

The desired result would be :

JavaScript

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

JavaScript

Sample DataFrame

JavaScript

Ouput

JavaScript

To create a boolean array

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement