首页 > 解决方案 > 将int数据类型转换为日期时间python

问题描述

我有一个包含用户 ID、评级和时间戳信息的数据集。原始数据集中的“时间戳”列是整数数据类型,我需要将其转换为日期数据类型。我的示例数据集如下:

| userId | rating | timestamp |
|--------|--------|-----------|
| 741    | 3.5    | 40006     |
| 742    | 3.5    | 41707     |
| 741    | 2.5    | 40279     |
| 743    | 3.5    | 41404     |
| 765    | 3.5    | 41684     |
| 800    | 2.5    | 42440     |
| 804    | 3      | 41540     |
| 802    | 4      | 41715     |
| 801    | 2.5    | 39916     |
| 741    | 3.5    | 41707     |

我尝试了下面的代码行,但所有时间戳都给了我一个日期“1970-01-01”。

df.loc[:,'timestamp'] = pd.to_datetime(df.loc[:,'timestamp'], format='%d%b%Y')

有人可以让我知道更正会是什么吗?

标签: pythonpandasdatetimepython-datetime

解决方案


推荐阅读