Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

pandas - Select a multiple-key cross section from a 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)

?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...