A similar question to Slice Pandas dataframe by index values that are (not) in a list, in the particular case of multi index slicers.
Sample data copied from help(df.loc)
:
import pandas
tuples = [
('cobra', 'mark i'), ('cobra', 'mark ii'),
('sidewinder', 'mark i'), ('sidewinder', 'mark ii'),
('viper', 'mark ii'), ('viper', 'mark iii')
]
index = pandas.MultiIndex.from_tuples(tuples)
values = [[12, 2], [0, 4], [10, 20],
[1, 4], [7, 1], [16, 36]]
df = pandas.DataFrame(values, columns=['max_speed', 'shield'], index=index)
Multi index slicing can be used to select all rows that contain "mark i" as their second index:
idx = pandas.IndexSlice
df.loc[idx[:, "mark i"],:]
I would like to inverse the selection. The result should contain all rows that do not contain "mark i" as their second index.