gpt4 book ai didi

python - 将日期时间格式转换为 Unix 时间戳 Pandas

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:53 24 4
gpt4 key购买 nike

我正在尝试将正常的日期时间转换为 pandas 中的 unix 时间戳。在寻找周围的一些样本时,我只能找到一个例子 here但我无法在我的上下文中使用。数据集没有标题,最后的 2 列 需要转换 UNIX 时间戳 并生成新的输出以及前 3 列。

1466f7b93975983f6e292a8a4faaa4b2,1619b4d0d283c0dddb17d24a359a3b49,36db348cde68592a31d502366fc52932,2010-03-08 17:09:00.472544,2010-03-12 16:09:58.122987
367c13356a5d22158f0ae56977134e2c,eedb7d0714796b64767a8710ea3844a7,925476200929fd346ea312cbe9a046fe,2010-03-08 17:08:29.174236,2010-03-12 16:09:58.122987
edf6b1e4f67b0e8a5080d299c9f9aeb2,7cb7681b90388a7522d0f06578591567,ffde0649a72ded8e33522c503a4d5cbe,2010-03-08 17:08:22.030524,2010-03-12 16:09:58.122987
6bb2ad8bc78897e99072d4d76cf0f19c,b644947ac4db03bdb518cfa71765f8c8,eb25089d396c06255cbb5f1bad801cc4,2010-03-08 17:07:55.819137,2010-03-12 16:09:58.122987

输入文件有几百万行,我在这里发布的只有几行。任何建议都是有值(value)的。提前致谢

最佳答案

可以先read_csv然后通过 astype 将最后两列转换为 np.int64除以 10**9。写入文件使用 to_csv :

import pandas as pd
import numpy as np
import io

temp=u"""1466f7b93975983f6e292a8a4faaa4b2,1619b4d0d283c0dddb17d24a359a3b49,36db348cde68592a31d502366fc52932,2010-03-08 17:09:00.472544,2010-03-12 16:09:58.122987
367c13356a5d22158f0ae56977134e2c,eedb7d0714796b64767a8710ea3844a7,925476200929fd346ea312cbe9a046fe,2010-03-08 17:08:29.174236,2010-03-12 16:09:58.122987
edf6b1e4f67b0e8a5080d299c9f9aeb2,7cb7681b90388a7522d0f06578591567,ffde0649a72ded8e33522c503a4d5cbe,2010-03-08 17:08:22.030524,2010-03-12 16:09:58.122987
6bb2ad8bc78897e99072d4d76cf0f19c,b644947ac4db03bdb518cfa71765f8c8,eb25089d396c06255cbb5f1bad801cc4,2010-03-08 17:07:55.819137,2010-03-12 16:09:58.122987"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),
header=None, #no header in csv
names=['a','b','c','d', 'e'], #set custom column names
parse_dates=['d','e']) #parse columns d, e to datetime
print df
a b \
0 1466f7b93975983f6e292a8a4faaa4b2 1619b4d0d283c0dddb17d24a359a3b49
1 367c13356a5d22158f0ae56977134e2c eedb7d0714796b64767a8710ea3844a7
2 edf6b1e4f67b0e8a5080d299c9f9aeb2 7cb7681b90388a7522d0f06578591567
3 6bb2ad8bc78897e99072d4d76cf0f19c b644947ac4db03bdb518cfa71765f8c8

c d \
0 36db348cde68592a31d502366fc52932 2010-03-08 17:09:00.472544
1 925476200929fd346ea312cbe9a046fe 2010-03-08 17:08:29.174236
2 ffde0649a72ded8e33522c503a4d5cbe 2010-03-08 17:08:22.030524
3 eb25089d396c06255cbb5f1bad801cc4 2010-03-08 17:07:55.819137

e
0 2010-03-12 16:09:58.122987
1 2010-03-12 16:09:58.122987
2 2010-03-12 16:09:58.122987
3 2010-03-12 16:09:58.122987


df['d'] = df['d'].astype(np.int64) // 10**9
df['e'] = df['e'].astype(np.int64) // 10**9
print df
a b \
0 1466f7b93975983f6e292a8a4faaa4b2 1619b4d0d283c0dddb17d24a359a3b49
1 367c13356a5d22158f0ae56977134e2c eedb7d0714796b64767a8710ea3844a7
2 edf6b1e4f67b0e8a5080d299c9f9aeb2 7cb7681b90388a7522d0f06578591567
3 6bb2ad8bc78897e99072d4d76cf0f19c b644947ac4db03bdb518cfa71765f8c8

c d e
0 36db348cde68592a31d502366fc52932 1268068140 1268410198
1 925476200929fd346ea312cbe9a046fe 1268068109 1268410198
2 ffde0649a72ded8e33522c503a4d5cbe 1268068102 1268410198
3 eb25089d396c06255cbb5f1bad801cc4 1268068075 1268410198

df.to_csv('filename', header=None, index=False)

关于python - 将日期时间格式转换为 Unix 时间戳 Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37192220/

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