如何使用日期索引和多级列进行切片 (MultiIndex)

How to slice with date-index and multilevel columns (MultiIndex)

我有一个大的 pd.DataFramea,看起来像:

bid        TIT IM Equity HELN SE Equity FHZN SE Equity GLEN LN Equity  
field         LAST_PRICE     LAST_PRICE     LAST_PRICE     LAST_PRICE   
currency             EUR            CHF            CHF            GBp   
2013-12-31           NaN            NaN            NaN        285.954   
2014-01-02        0.7085            NaN            NaN        283.942   
    ...             ...            ...           ...            ...
2014-01-08        0.7990         422.65         511.46        287.875   
2014-01-09        0.8095         421.26         514.35        283.165   
2014-01-10        0.8135         423.35         514.83        291.487   
2014-01-13        0.8065         417.78         515.79        291.670   

index 是 dates,我有 3 个级别的 MultiIndex 列(bidfieldcurrency)。

如何切片:

  1. 得到一个 DataFrame 的所有值,其中 currency == CHFdate in ['2014-01-08' : '2014-01-10']
  2. 获得一个 DataFrame,其中出价以 HEcurrency == CHFEURdate in ['2014-01-08' : '2014-01-10']
  3. 开头的所有值

我在文档中找不到它。

您可以使用 xs or loc:

print df['2014-01-08' : '2014-01-10'].xs('CHF',level=2,axis=1)

bid        HELN SE Equity FHZN SE Equity
field          LAST_PRICE     LAST_PRICE
2014-01-08         422.65         511.46
2014-01-09         421.26         514.35
2014-01-10         423.35         514.83

#sort multicolumns
df = df.sort_index(axis=1)

he = tuple([s for s in df.columns.levels[0].tolist() if 'HE' in s[:2]])
print he
#('HELN SE Equity',)

print df.loc['2014-01-08' : '2014-01-10', (he, slice(None), ['CHF','EUR'])]

bid        HELN SE Equity
field          LAST_PRICE
currency              CHF
2014-01-08         422.65
2014-01-09         421.26
2014-01-10         423.35

Docs.