I have a pandas data frame with a large MultiIndex. I'm selecting columns from this dataframe with various metadata that is in the index, like for example
current_row = df.xs(number, level='counter', drop_level=False, axis=1)
So far, so good. However, number
comes from a list that might contain numbers that are not contained in the counter
level in the index, so the above obviously fails with a KeyError.
So is there any way to test if my number exists, so that I can either continue with the number, or throw a custom error and continue with the next number?
isin
sounds like it would be what I need, but I can't get it to work on my Multiindex.