[英]pandas dataframe index remove date from datetime
文件 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 dataframe ,其中索引列只有格式為'%H:%M:%S'
的時間部分,沒有日期。
import pandas as pd
df = pd.read_csv("dataexample_df.txt", sep=' ', header=None, index_col=0)
print(df)
Output:
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
但是,想要 output:
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(參見Pandas 中 Parse_dates 中的答案),但只收到錯誤消息。 此外,有些相關的是Python/Pandas 僅將字符串轉換為時間,但沒有運氣,我被卡住了。 我正在使用 Python 3.7。
考慮到你的df
是這樣的:
In [121]: df
Out[121]:
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
您可以將Series.replace
與Series.dt.time
一起使用:
In [122]: df.reset_index(inplace=True)
In [127]: df[0] = pd.to_datetime(df[0].str.replace('_', ' ')).dt.time
In [130]: df.set_index(0, inplace=True)
In [131]: df
Out[131]:
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
在這里,我創建了一個簡單的 function 來格式化您的日期時間列,請試試這個。
import pandas as pd
df = pd.read_csv('data.txt', sep=" ", header=None)
def format_time(date_str):
# split date and time
time = iter(date_str.split('_')[1])
# retun the time value adding
return ':'.join(a+b for a,b in zip(time, time))
df[0] = df[0].apply(format_time)
print(df)
您需要使用format
參數告訴它您的日期格式是什么(否則您會收到錯誤消息):
# gives an error:
pd.to_datetime('2020-12-04_163024')
# works:
pd.to_datetime('2020-12-04_163024', format=r'%Y-%m-%d_%H%M%S')
因此,您可以將其應用於您的 dataframe ,然后使用dt.time
訪問時間:
df['time'] = pd.to_datetime(df.index, format=r'%Y-%m-%d_%H%M%S').dt.time
這將為您提供 object 的時間,但如果您想格式化它,只需使用以下內容:
df['time'] = df['time'].strftime('%H:%M:%S')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.