Select a multiple-key cross section from a DataFrame

There are better ways of doing this with more recent versions of Pandas (see Multi-indexing using slicers in the changelog for version 0.14):

regression_df.loc[(slice(None), ['SPY', 'GLD']), :]

This can be made more readable with the use of pd.IndexSlice:

df.loc[pd.IndexSlice[:, ['SPY', 'GLD']], :]

With the convention idx = pd.IndexSlice, this becomes

df.loc[idx[:, ['SPY', 'GLD']], :]

Leave a Comment