0

I have a data frame like this:

Date
 9/02/2019
12/08/2019
 8/06/2019

I want to add a 0 in infront of dates that are in single digit. I want it to be like this:

Date
09/02/2019
12/08/2019
08/06/2019

I am using this RegEx and the string manipulation. The string manipulation by itself works, but when I try to work it out with the RegEx, it doesn't yield anything.

if row['Date'] == r'[\d]{1}/[\d]{1,2}/[\d]{4}':
   row['blah'] = '0' + row['Date']
   print(row['blah'])
else:
   pass
Bharath
  • 559
  • 11
  • 27

1 Answers1

2

Use pd.to_datetime and convert it to string using Series.dt.strftime:

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
df['Date'] = df['Date'].dt.strftime('%d/%m/%Y')

print(df)
         Date
0  09/02/2019
1  12/08/2019
2  08/06/2019
Space Impact
  • 13,085
  • 23
  • 48