【发布时间】:2021-03-19 21:12:00
【问题描述】:
文件dataexample_df.txt:
2020-12-04_163024 26.15 26.37 19.40 24.57
2020-12-04_163026 26.15 26.37 19.20 24.57
2020-12-04_163028 26.05 26.37 18.78 24.57
我想将其作为 pandas 数据框读取,其中索引列只有'%H:%M:%S' 格式的时间部分,没有日期。
import pandas as pd
df = pd.read_csv("dataexample_df.txt", sep=' ', header=None, index_col=0)
print(df)
输出:
1 2 3 4
0
2020-12-04_163024 26.15 26.37 19.40 24.57
2020-12-04_163026 26.15 26.37 19.20 24.57
2020-12-04_163028 26.05 26.37 18.78 24.57
但是,想要的输出:
1 2 3 4
0
16:30:24 26.15 26.37 19.40 24.57
16:30:26 26.15 26.37 19.20 24.57
16:30:28 26.05 26.37 18.78 24.57
我尝试了不同的date_parser= -functions(参见Parse_dates in Pandas 中的答案)
但只收到错误消息。另外,Python/Pandas convert string to time only 有点相关,但运气不好,我被卡住了。我正在使用 Python 3.7。
【问题讨论】: