将时间戳列转换为浮动分钟熊猫python

时间:2020-06-17 16:56:10

标签: pandas dataframe timestamp

我有一个数据框,其中的一列包含时间戳,如下所示:

    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.....

1 个答案:

答案 0 :(得分:0)

您可以转换为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
相关问题