Change date format of pandas column (month-day-year to day-month-year)

前端 未结 2 838
失恋的感觉
失恋的感觉 2021-01-24 04:20

Got the following issue.

I have an column in my pandas with some dates and some empty values.

Example:

    1 - 3-20-2019
    2 - 
    3 - 2-25-2         


        
2条回答
  •  孤城傲影
    2021-01-24 05:01

    Not sure if this is the fastest way to get it done. Anyway,

    df = pd.DataFrame({'Date': {0: '3-20-2019', 1:"", 2:"2-25-2019"}}) #your dataframe
    df['Date'] = pd.to_datetime(df.Date) #convert to datetime format
    df['Date'] = [d.strftime('%d-%m-%Y') if not pd.isnull(d) else '' for d in df['Date']]
    

    Output:

             Date
    0  20-03-2019
    1            
    2  25-02-2019
    

提交回复
热议问题