如何过滤多索引数据框上的日期

How to filter dates on multiindex dataframe

我正在寻找一种方法来按星期几 and/or selected 日期过滤多索引数据框,如下所示。假设我需要

有什么方法可以做到?

                Col1        Col2     Col3    Col4
Date        Two 
2015-05-14  10   81.370003  6.11282  39.753  44.950001
            11   80.419998  6.03380  39.289  44.750000
            C3   80.879997  6.00746  41.249  44.360001
2015-05-19   3   80.629997  6.10465  41.047  40.980000
            S9   80.550003  6.14370  41.636  42.790001
2015-05-21  19   80.480003  6.16096  42.137  43.680000
2015-05-22  C3   80.540001  6.13916  42.179  43.490002

如果您有 Date 作为 datetime 类型,您可以只使用 dayofweek 获取星期几并根据它进行查询。

Select 仅限星期一:

df[df.index.get_level_values('Date').dayofweek == 0]

Select 天,周一和周五除外:

import numpy as np
df[np.in1d(df.index.get_level_values('Date').dayofweek, [1,2,3,5,6])]

#                    Col1      Col2   Col3       Col4
#      Date Two             
#2015-05-14 10  81.370003   6.11282 39.753  44.950001
#           11  80.419998   6.03380 39.289  44.750000
#           C3  80.879997   6.00746 41.249  44.360001
#2015-05-19 3   80.629997   6.10465 41.047  40.980000
#           S9  80.550003   6.14370 41.636  42.790001
#2015-05-21 19  80.480003   6.16096 42.137  43.680000