Reputation: 311
I have this date data in my excel file:
18-09-14
18-09-17
18-09-18
My problem is how can I make the year format into 4-digit
format, like this:
2018-09-14
2018-09-17
2018-09-18
I tried .to_datetime
and .strftime
of pandas but it throws me an error. Is there any other way to solve this? Any idea, thank you so much!
Upvotes: 0
Views: 44
Reputation: 13255
First, convert the string to datetime
datatype using to_datetime
and parameter yearfirst=True
then use strftime("%Y-%m-%d")
as:
pd.to_datetime('18-09-14',yearfirst=True).strftime("%Y-%m-%d")
'2018-09-14'
Or if it is a dataframe then:
pd.to_datetime(df[0],yearfirst=True).dt.strftime("%Y-%m-%d")
0 2018-09-14
1 2018-09-17
2 2018-09-18
Name: 0, dtype: object
Upvotes: 1