Python,pandas,multiindex上的boolean indexer

时间:2017-08-05 12:55:59

标签: python pandas indexing multi-index

让我们考虑以下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)

enter image description here

我想使用布尔索引器选择某些行: 在这里,我想使用dayofweek!=5仅选择在一周的特定日期发生的行。

我该怎么做?

1 个答案:

答案 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