MultiIndex-based indexing in pandas
xs may be what you want. Here are a few examples:
In [63]: df.xs(('B',), level='Alpha')
Out[63]:
I II III IV V VI VII
Int Bool
0 True -0.430563 0.139969 -0.356883 -0.574463 -0.107693 -1.030063 0.271250
False 0.334960 -0.640764 -0.515756 -0.327806 -0.006574 0.183520 1.397951
1 True -0.450375 1.237018 0.398290 0.246182 -0.237919 1.372239 -0.805403
False -0.064493 0.967132 -0.674451 0.666691 -0.350378 1.721682 -0.791897
2 True 0.143154 -0.061543 -1.157361 0.864847 -0.379616 -0.762626 0.645582
False -3.253589 0.729562 -0.839622 -1.088309 0.039522 0.980831 -0.113494
In [64]: df.xs(('B', False), level=('Alpha', 'Bool'))
Out[64]:
I II III IV V VI VII
Int
0 0.334960 -0.640764 -0.515756 -0.327806 -0.006574 0.183520 1.397951
1 -0.064493 0.967132 -0.674451 0.666691 -0.350378 1.721682 -0.791897
2 -3.253589 0.729562 -0.839622 -1.088309 0.039522 0.980831 -0.113494
Edit:
For the last requirement you can chain get_level_values
and isin
:
Get the even values in the index (other ways to do this too)
In [87]: ix_vals = set(i for _, i, _ in df.index if i % 2 == 0)
ix_vals
Out[87]: set([0L, 2L])
Use these with isin
In [89]: ix = df.index.get_level_values('Int').isin(ix_vals)
In [90]: df[ix]
Out[90]: I II III IV V VI VII
Alpha Int Bool
A 0 True -1.315409 1.203800 0.330372 -0.295718 -0.679039 1.402114 0.778572
False 0.008189 -0.104372 0.419110 0.302978 -0.880262 -1.037645 -0.264265
2 True -2.414290 0.896990 0.986167 -0.527074 0.550753 -0.302920 0.228165
False 1.275831 0.448089 -0.635874 -0.733855 -0.747774 -1.108976 0.151474
B 0 True -0.430563 0.139969 -0.356883 -0.574463 -0.107693 -1.030063 0.271250
False 0.334960 -0.640764 -0.515756 -0.327806 -0.006574 0.183520 1.397951
2 True 0.143154 -0.061543 -1.157361 0.864847 -0.379616 -0.762626 0.645582
False -3.253589 0.729562 -0.839622 -1.088309 0.039522 0.980831 -0.113494
You can use pd.IndexSlice for an intuitive way (Inspired from this answer). Some examples (using pandas 0.18.0):
df.sort_index(inplace=True)
idx = pd.IndexSlice
evens = np.arange(2,max(df.index.levels[1])+1,2)
df.loc[idx[['A','B'],evens,True],['III','V']]
Out[]:
III V
Alpha Int Bool
A 2 True -1.041243 -0.561155
B 2 True 0.381918 -0.148990
df.loc[idx[:,evens,:],:]
Out[]:
I II III IV V VI \
Alpha Int Bool
A 2 False 0.791142 0.333383 0.089767 -0.584465 0.295676 -1.323792
True -1.023160 -0.442004 -1.041243 1.613184 -0.561155 0.397923
B 2 False 0.383229 -0.052715 -0.214347 -2.041429 -1.101059 -0.374035
True -0.183386 -0.855367 0.381918 -0.261106 -0.148990 0.621537
VII
Alpha Int Bool
A 2 False 0.717301
True -0.133701
B 2 False 0.166314
True 0.517513