【问题标题】:Creating multiple dataframes based on date of timestamp in Python根据 Python 中的时间戳日期创建多个数据帧
【发布时间】:2021-12-02 11:04:42
【问题描述】:

我正在寻找一种优雅的解决方案来创建具有混合时间戳的多个数据帧

我有一个 dataframe 有超过 16 列的数千行,其中一列的时间戳值如下:

2021-09-28 00:00:00 ~ 2021-09-28 23:59:59

另外,时间戳值在数据帧上没有连续使用,并且混淆了时间戳。

示例

IN [1] : df["datetime"][24082:24085]
OUT [1] :
24082   2021-09-28 07:25:21.446
24083   2021-09-28 07:25:22.444
24084   2021-10-01 19:49:40.549
24085   2021-10-01 19:49:41.549

我要做的是从数据帧创建多个数据帧,这取决于时间戳的日期,例如;

df1 is the rows with 2021-09-28 00:00:00.001 ~ 23:59:59.999
df2 is the rows with 2021-09-29 00:00:00.001 ~ 23:59:59.999
df3 is the rows with 2021-09-30 00:00:00.001 ~ 23:59:59.999
     ...
dfn is the rows with latest date

如何实现上述数据框?

【问题讨论】:

    标签: python pandas dataframe datetime group-by


    【解决方案1】:

    groupbyGrouper 一起使用:

    for date, data in df.groupby(pd.Grouper(key='datetime', freq='D')):
        # do_something_with_your_data
        print(data)
    

    【讨论】:

      猜你喜欢
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2020-11-02
      • 2019-05-30
      • 1970-01-01
      • 2021-08-09
      相关资源
      最近更新 更多