【问题标题】:Subsetting Pandas Datetime Column (With Time Stamp) to Show Only Rows Before 3AM将 Pandas 日期时间列(带时间戳)设置为仅显示凌晨 3 点之前的行
【发布时间】:2018-12-14 02:02:41
【问题描述】:

我有以下示例熊猫数据框:

ID   Datetime_Col
1    2014-08-08 00:32:00
2    2016-01-25 23:25:00
3    2018-05-04 06:04:00

我想对其进行子集化,以便仅显示凌晨 3 点之前日期时间的行。

我最初尝试从 datetime 中提取小时并从中创建一个新列到稍后的子集 .... 但我认为有更有效的方法吗?

【问题讨论】:

    标签: python pandas datetime time subset


    【解决方案1】:

    不需要新列,按dt.yearboolean indexing 过滤:

    df = df[df['Datetime_Col'].dt.hour < 3]
    print (df)
       ID        Datetime_Col
    0   1 2014-08-08 00:32:00
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2016-10-05
      • 1970-01-01
      • 2021-01-18
      • 2019-01-28
      • 2021-04-06
      • 2018-06-20
      相关资源
      最近更新 更多