Home > Mobile >  Pandas Dataframe date reformat
Pandas Dataframe date reformat

Time:06-14

I have a pandas dataframe (image supplied) where there is inconsistent dates, I need to add 20 after the second bracket, is there a formula I can apply to do this?

enter image description here

CodePudding user response:

Use pd.to_datetime and dayfirst=True in order to remove any ambiguity: "13/08/16" can be interpreted both like "2016-08-13" or like "2013-08-16"

# df is eplData
df['date2'] = pd.to_datetime(df['date'], dayfirst=True).dt.strftime('%d/%m/%Y')
print(df)

# Output
         date       date2
0  13/05/2018  13/05/2018
1    13/08/16  13/08/2016

CodePudding user response:

You should convert that column into a datetime column like this:

eplData['column_name'] = pd.to_datetime(eplData['column_name'])

There are also specific ways you can format the datetime column. More info here

  • Related