Python - convert date string from YYYY-MM-DD to DD-MMM-YYYY using datetime?

前端 未结 2 1293
不思量自难忘°
不思量自难忘° 2021-01-23 11:43

So I have read a number of threads on this, and am still stumped. Any help would be sincerely appreciated.

I have a column in a dataframe that contains strings of dates,

相关标签:
2条回答
  • 2021-01-23 12:11

    No need apply by using pd.to_datetime

    pd.to_datetime(df_combined['VisitDate'],errors='coerce',format='%d-%b-%Y').fillna('')
    
    0 讨论(0)
  • 2021-01-23 12:23

    You probably just need to assign the result back to the column itself:

    df_combined['VisitDate'] = df_combined['VisitDate'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').strftime('%d-%b-%Y') if x != "" else "")
    
    0 讨论(0)
提交回复
热议问题