Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a multiple-key cross section from a DataFrame

Tags:

People also ask

How do I select multiple columns with Loc?

Using df[] & loc[] to Select Multiple Columns by Name loc[] you can select multiple columns by names or labels. To select the columns by names, the syntax is df.

Can you slice a DataFrame?

Slicing a DataFrame in Pandas includes the following steps: Ensure Python is installed (or install ActivePython) Import a dataset. Create a DataFrame. Slice the DataFrame.


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)  

?