我有一个大pd.DataFrame
,a
,看起来像:
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
索引为dates
,我的MultiIndex列有3个级别(bid
,field
,currency
)。
我如何切片:
DataFrame
所有值currency == CHF
和date in ['2014-01-08' : '2014-01-10']
DataFrame
,其中包含出价以HE
和currency == CHF
或EUR
和date in ['2014-01-08' : '2014-01-10']
我在文档中找不到它。
答案 0 :(得分:2)
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