简体   繁体   中英

How can I convert following string date-time into pandas datetime

How can I convert the following string format of datetime into datetime object to be used in pandas Dataframe? I tried many examples, but it seems my format is different from the standard Pandas datetime object. I know this could be a repetition, but I tried solutions on the Stackexchange, but they don't work!

在此处输入图像描述

Below code will convert it into appropriate format

df = pd.DataFrame({'datetime':['2013-11-1_00:00','2013-11-1_00:10','2013-11-1_00:20']})
df['datetime_changed'] = pd.to_datetime(df['datetime'].str.replace('_','T'))
df.head()

output:

在此处输入图像描述

您可以使用带formatpd.to_datetime

df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d_%H:%M')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM