pandas MultiIndex Select from MultiIndex by Level


Example

Given the following DataFrame:

In [11]: df = pd.DataFrame(np.random.randn(6, 3), columns=['A', 'B', 'C'])

In [12]: df.set_index(['A', 'B'], inplace=True)

In [13]: df
Out[13]: 
                            C
A         B                  
 0.902764 -0.259656 -1.864541
-0.695893  0.308893  0.125199
 1.696989 -1.221131 -2.975839
-1.132069 -1.086189 -1.945467
 2.294835 -1.765507  1.567853
-1.788299  2.579029  0.792919

Get the values of A, by name:

In [14]: df.index.get_level_values('A')
Out[14]: 
Float64Index([0.902764041011, -0.69589264969,  1.69698924476, -1.13206872067,
               2.29483481146,   -1.788298829],
             dtype='float64', name='A')

Or by number of level:

In [15]: df.index.get_level_values(level=0)
Out[15]: 
Float64Index([0.902764041011, -0.69589264969,  1.69698924476, -1.13206872067,
               2.29483481146,   -1.788298829],
             dtype='float64', name='A')

And for a specific range:

In [16]: df.loc[(df.index.get_level_values('A') > 0.5) & (df.index.get_level_values('A') < 2.1)]
Out[16]:
                           C
A        B                  
0.902764 -0.259656 -1.864541
1.696989 -1.221131 -2.975839

Range can also include multiple columns:

In [17]: df.loc[(df.index.get_level_values('A') > 0.5) & (df.index.get_level_values('B') < 0)]
Out[17]: 
                           C
A        B                  
0.902764 -0.259656 -1.864541
1.696989 -1.221131 -2.975839
2.294835 -1.765507  1.567853

To extract a specific value you can use xs (cross-section):

In [18]: df.xs(key=0.9027639999999999)
Out[18]:
                  C
B
-0.259656 -1.864541

In [19]: df.xs(key=0.9027639999999999, drop_level=False)
Out[19]:
                           C
A        B
0.902764 -0.259656 -1.864541