Select a multiple-key cross section from a DataFrame

pandas

Solution

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']], :]

Problem

I have a DataFrame "df" with (time,ticker) Multiindex and bid/ask/etc data columns: ``` tod last bid ask volume time ticker 2013-02-01 SPY 1600 149.70 150.14 150.17 1300 SLV 1600 30.44 30.38 30.43 3892 GLD 1600 161.20 161.19 161.21 3860 ``` I would like to select a second-level (level=1) cross section using multiple keys. Right now, I can do it using one key, i.e. ``` df.xs('SPY', level=1) ``` which gives me a timeseries of SPY. What is the best way to select a multi-key cross section, i.e. a combined cross-section of both SPY and GLD, something like: ``` df.xs(['SPY', 'GLD'], level=1) ``` ?

Original source