pandas dataframe select columns in multiindex [duplicate]
There is a get_level_values
method that you can use in conjunction with boolean indexing to get the the intended result.
In [13]:df = pd.DataFrame(np.random.random((4,4)))df.columns = pd.MultiIndex.from_product([[1,2],['A','B']])print df 1 2 A B A B0 0.543980 0.628078 0.756941 0.6988241 0.633005 0.089604 0.198510 0.7835562 0.662391 0.541182 0.544060 0.0593813 0.841242 0.634603 0.815334 0.848120In [14]:print df.iloc[:, df.columns.get_level_values(1)=='A'] 1 2 A A0 0.543980 0.7569411 0.633005 0.1985102 0.662391 0.5440603 0.841242 0.815334
Method 1:
df.xs('A', level='Col', axis=1)
for more refer to http://pandas.pydata.org/pandas-docs/stable/advanced.html#cross-section
Method 2:
df.loc[:, (slice(None), 'A')]
Caveat: this method requires the labels to be sorted. for more refer to http://pandas.pydata.org/pandas-docs/stable/advanced.html#the-need-for-sortedness-with-multiindex
EDIT*Best way now is to use indexSlice for multi-index selections
idx = pd.IndexSliceA = df.loc[:,idx[:,'A']]B = df.loc[:,idx[:,'B']]