Red Vibes
Red Vibes

Reputation: 79

Convert Month in a date to its respective Numbers in pandas

I have a column consist of dates

df=pd.DataFrame({'Date':['1 June'13' , '1 Nov'15' , '1 Dec'20']})
Date
1 June'13
1 Nov'15
1 Dec'20

I want to convert this date to this format-

1-06-2013

What I have tried-

df['Date']=pd.to_datetime(df['Date']).dt.strftime('%d-%b-%y')

what I get-

01-Jun-2013

Is there any way to get this o/p with a small and simple code Thanks in advance!!!

Upvotes: 0

Views: 64

Answers (1)

Pablo C
Pablo C

Reputation: 4761

You can achieve it with the proper date format code and using Series.str.lstrip to remove the zero-padded:

pd.to_datetime(df.Date).dt.strftime("%d-%m-%Y").str.lstrip('0')
#0    1-06-2013
#1    1-11-2015
#2    1-12-2020

Upvotes: 1

Related Questions