让我们考虑以下pandas DataFrame:
from pandas import Timestamp
dic={'volume': {('CSC', Timestamp('2016-08-06 00:00:00'), 'CSCF7'): 0,
('CSC', Timestamp('2016-08-07 00:00:00'), 'CSCG7'): 0,
('CSC', Timestamp('2016-08-08 00:00:00'), 'CSCH7'): 0,
('DA', Timestamp('2016-08-06 00:00:00'), 'DCF7'): 0,
('DA', Timestamp('2016-08-07 00:00:00'), 'DCG7'): 0,
('DA', Timestamp('2016-08-08 00:00:00'), 'DCH7'): 0,
('GF', Timestamp('2016-08-06 00:00:00'), 'GFF7'): 0,
('GF', Timestamp('2016-08-07 00:00:00'), 'GFH7'): 0,
('GF', Timestamp('2016-08-08 00:00:00'), 'GFJ7'): 0}}
import pandas as pd
df=pd.DataFrame(dic)
我想使用布尔索引器选择某些行:
在这里,我想使用dayofweek!=5
仅选择在一周的特定日期发生的行。
我该怎么做?
答案 0 :(得分:2)
在get_level_values
上使用df.index
即可
In [367]: df.loc[df.index.get_level_values(1).dayofweek != 5]
Out[367]:
volume
CSC 2016-08-07 CSCG7 0
2016-08-08 CSCH7 0
DA 2016-08-07 DCG7 0
2016-08-08 DCH7 0
GF 2016-08-07 GFH7 0
2016-08-08 GFJ7 0