Let's say I have a multi-indexed pandas dataframe that looks like the following one, taken from the documentation.
import numpy as np
import pandas as pd
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
Which looks like this:
0 1 2 3
bar one -0.096648 -0.080298 0.859359 -0.030288
two 0.043107 -0.431791 1.923893 -1.544845
baz one 0.639951 -0.008833 -0.227000 0.042315
two 0.705281 0.446257 -1.108522 0.471676
foo one -0.579483 -2.261138 -0.826789 1.543524
two -0.358526 1.416211 1.589617 0.284130
qux one 0.498149 -0.296404 0.127512 -0.224526
two -0.286687 -0.040473 1.443701 1.025008
Now I only want the rows where "ne" is contained in second level of the MultiIndex.
Is there any way to slice the MultiIndex for (partly) contained strings?
You can apply a mask like:
which returns:
EDIT: It is possible also applying a logical mask on multiple levels, e.g.:
returns: