Pandas - Converting date column from dd/mm/yy hh:mm:ss to yyyy-mm-dd hh:mm:ss

后端 未结 2 527
旧巷少年郎
旧巷少年郎 2021-02-08 22:54

I have a dataframe (df) that has a date column (column name : sale_date) that stores data in the below format

dd/mm/yy hh:mm:ss

I am trying to

2条回答
  •  一个人的身影
    2021-02-08 23:40

    If you know you will have a consistent format in your column, you can pass this to to_datetime:

    df['sale_date'] = pd.to_datetime(df['sale_date'], format='%d/%m/%y %H:%M:%S')
    

    If your formats aren't necessarily consistent but do have day before month in each case, it may be enough to use dayfirst=True though this is difficult to say without seeing the data:

    df['sale_date'] = pd.to_datetime(df['sale_date'], dayfirst=True)
    

提交回复
热议问题