简体   繁体   中英

Python change a date format in dataframe

I have a dataset containing a column "date":

date                     item
20.3.2010 17:08           a 
20.3.2010 11:16           b
2010-03-20 15:55:14.060   c
2010-03-21 13:56:45.077   d

I would like to convert all values that have format as 20.3.2010 17:08 into 2010-03-21 13:56:45.077 .

Does anybody have an idea?

Thank you.

df['date'] = pd.to_datetime(df['date'], , format = '%Y-%m-%d %H:%M:%S.%f')

您可以在此处找到有关pd.to_datetime()更多信息,并且可以在此处找到格式字符串类型。

Check on below:

from datetime import datetime

INPUT_FORMAT = '%d.%m.%Y %H:%M'
OUTPUT_FORMAT = '%Y-%m-%d %H:%M:%S.%f'

datetime.strptime('20.3.2010 17:08',INPUT_FORMAT).strftime(OUTPUT_FORMAT)
#Output '2010-03-20 17:08:00.000000'

You could find more information in offcial strptime and strftime .

To do a 100% match with 3 digits microseconds you could use this SO approach .

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