Blessing Bribiesca
Blessing Bribiesca

Reputation: 47

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?

Upvotes: 1

Views: 161

Answers (2)

Ankit Kumar
Ankit Kumar

Reputation: 51

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.

Upvotes: 0

QuantumEngineer
QuantumEngineer

Reputation: 121

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

Upvotes: 2

Related Questions