When I have a pandas.DataFrame df with columns ["A", "B", "C", "D"], I can filter it using constructions like df[df["B"] == 2].
How do I do the equivalent of df[df["B"] == 2], if B is the name of a level in a MultiIndex instead? (For example, obtained by df.groupby(["A", "B"]).mean() or df.setindex(["A", "B"]))
I would suggest either:
df.xs(2, level='B')or
df[df.index.get_level_values('B') == val]I’d like to make the syntax for the latter operation a little nicer.