【问题标题】:Screen out data in specific periods of time筛选特定时间段内的数据
【发布时间】:2020-08-07 17:43:42
【问题描述】:

原来的'df'是:

                     value   id                                                                       
2014-02-28 09:00:00  10660    a
2014-02-28 09:01:00  10697    a
2014-02-28 09:02:00  10672    b
2014-02-28 09:03:00  10667    b
2014-02-28 09:04:00  10650    c
...
2020-04-10 14:55:00   6983    d
2020-04-10 14:56:00   6988    d
2020-04-10 14:57:00   6991    e
2020-04-10 14:58:00   6979    e
2020-04-10 14:59:00   6984    f

索引的格式是'YY-MM-DD HH:MM:SS'。它的类型是'pandas.core.indexes.datetimes.DatetimeIndex'。

df的时间包含从2014年到2020年的每一天。

我想查找每年 1 月、2 月、3 月和 4 月的所有数据。抱歉,我无法显示预期的输出。

【问题讨论】:

    标签: python pandas datetime


    【解决方案1】:

    boolean indexing 中使用DatetimeIndex.monthIndex.isin

    df = df[df.index.month.isin([1,2,3,4])]
    

    【讨论】:

    • 谢谢。我只知道 pandas.Series.dt.month 之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2019-11-27
    • 2020-09-15
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多