【问题标题】:Convert timestamp column to float minutes pandas python将时间戳列转换为浮点分钟熊猫 python
【发布时间】:2020-06-17 16:56:10
【问题描述】:

我有一个数据框,其中一列包含时间戳:

    created
0   2020-05-27T14:00:00.000Z
1   2020-05-27T14:01:00.000Z
2   2020-05-27T14:01:00.000Z
3   2020-05-27T14:02:00.000Z
4   2020-05-27T14:02:00.000Z
... ...
992 2020-05-28T11:57:00.000Z
995 2020-05-28T11:58:00.000Z
997 2020-05-28T11:59:00.000Z
996 2020-05-28T11:59:00.000Z
998 2020-05-28T11:59:00.000Z

我想将所有此列转换为第一个事件后经过的分钟数的浮点数;例如

    created
0   0.0
1   1.0
2   1.0
3   2.0
4   2.0
... ...
and so on.....

【问题讨论】:

    标签: pandas dataframe timestamp


    【解决方案1】:

    你可以转换成datetime,减去和除以分钟:

    df['created'] = pd.to_datetime(df['created'])
    df['created'] = (df['created'] - df['created'].iloc[0])/pd.to_timedelta('1Min')
    

    输出:

         created
    0        0.0
    1        1.0
    2        1.0
    3        2.0
    4        2.0
    992   1317.0
    995   1318.0
    997   1319.0
    996   1319.0
    998   1319.0
    

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 2018-09-23
      • 2020-04-16
      • 1970-01-01
      • 2019-05-14
      • 2021-05-06
      • 2019-04-26
      • 2021-09-23
      • 2019-02-17
      相关资源
      最近更新 更多