简体   繁体   中英

How to change date format in python with pandas

I'm working with big data in pandas and I have a problem with the format of the dates, this is the format of one column

Wed Feb 24 12:06:14 +0000 2021

and I think it is easier to change the format of all the columns with a format like this

'%d/%m/%Y, %H:%M:%S'

how can i do that?

Does this work for you?

pandas.to_datetime(s, format='%d/%m/%Y, %H:%M:%S')   

Source: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

You can use the following function for your dataset.

def change_format(x):
format = dt.datetime.strptime(x, "%a %b %d %H:%M:%S %z  %Y")
new_format = format.strftime('%d/%m/%Y, %H:%M:%S')
return new_format

Then apply it using df['date_column'] = df['date_column'].apply(change_format) . Here df is your dataset.

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