gpt4 book ai didi

python - 合并具有偏移量的数据帧行(同一天生成但具有不同的时间戳)

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:31 25 4
gpt4 key购买 nike

我正在尝试合并多个数据帧,每个帧中都有诸如

之类的数据
timestamp   cap
0 1387118554000 3488670
1 1387243928000 1619159
2 1387336027000 2191987
3 1387435314000 4299421
4 1387539459000 9866232

每个值代表每日生成的数据,但是每个值不是在完全相同的毫秒生成的,因此时间戳不会合并。我需要一种方法将时间戳转换为年、月和日部分。然后我将能够合并我的数据集(除非有另一种方法来解决此类问题)。

最佳答案

您可以尝试to_datetime :

print pd.to_datetime(df['timestamp'], unit='ms')
0 2013-12-15 14:42:34
1 2013-12-17 01:32:08
2 2013-12-18 03:07:07
3 2013-12-19 06:41:54
4 2013-12-20 11:37:39
Name: timestamp, dtype: datetime64[ns]

df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
print df
timestamp cap
0 2013-12-15 14:42:34 3488670
1 2013-12-17 01:32:08 1619159
2 2013-12-18 03:07:07 2191987
3 2013-12-19 06:41:54 4299421
4 2013-12-20 11:37:39 9866232

然后你可以使用dt.date :

df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms').dt.date
print df
timestamp cap
0 2013-12-15 3488670
1 2013-12-17 1619159
2 2013-12-18 2191987
3 2013-12-19 4299421
4 2013-12-20 9866232

或者dt.strftime :

df['timestamp1'] = pd.to_datetime(df['timestamp'], unit='ms').dt.strftime('%Y-%m-%d')
print df
Int64Index([0, 1, 2, 3, 4], dtype='int64')
timestamp cap timestamp1
0 1387118554000 3488670 2013-12-15
1 1387243928000 1619159 2013-12-17
2 1387336027000 2191987 2013-12-18
3 1387435314000 4299421 2013-12-19
4 1387539459000 9866232 2013-12-20

关于python - 合并具有偏移量的数据帧行(同一天生成但具有不同的时间戳),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35228472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com