我有一个巨大的数据集,我试图将时间戳列解析为以下格式:YYYY-MM-DD HH:MM:SS:DEC,但我在数据帧中的输出类似于:210309_131046520_11。
因此:我想拆分输出列,使其具有以下列:
date, day, month, year, dec, hh, mm, ss例如:210309_131046520_11
date 21-03-09
day 09
month 03
year 2021
dec 520
hh 13
mm 10
ss 46
我尝试使用datetime模块,但似乎没有任何帮助。有人能帮帮忙吗?
我的时间戳列示例:
0 210309_131046520_11.sgy
1 210309_131046520_05.sgy
2 210309_131046528_02.sgy
3 210309_131046528_12.sgy
4 210309_131049712_07.sgy
...
162 210309_132658584_07.sgy
163 210309_132659632_10.sgy
164 210309_132701232_05.sgy
165 210309_132704256_02.sgy
166 210309_132707136_12.sgymy date列示例:
0 131046520
1 131046520
2 131046528
3 131046528
4 131049712
...
162 132658584
163 132659632
164 132701232
165 132704256
166 132707136我尝试了一件事:
使用split
s_df["date"] = s_df["timestamp"].apply(lambda row: row.split("_")[1])输出:
TypeError: list indices must be integers or slices, not str发布于 2021-04-04 23:05:50
您可以使用datetime的strptime来解释标准库中的日期:
from datetime import datetime as dt
dt.strptime('210309_131045620', '%y%m%d_%H%M%S%f')
Out: datetime.datetime(2021, 3, 9, 13, 10, 45, 620000)在pandas数据帧中,这是使用内置更有效地实现的,它遵循相同的格式化语法:
import pandas as pd
pd.to_datetime(df['date'], format='%y%m%d_%H%M%S%f')有关自定义格式字符串的详细信息,请参阅strftime参考:https://strftime.org
https://stackoverflow.com/questions/66942490
复制相似问题