How to specify date format when using pandas.to_csv?

前端 未结 3 776
囚心锁ツ
囚心锁ツ 2020-11-29 00:57

The default output format of to_csv() is:

12/14/2012  12:00:00 AM

I cannot figure out how to output only the date part with sp

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 01:12

    You could use strftime to save these as separate columns:

    df['date'] = df['datetime'].apply(lambda x: x.strftime('%d%m%Y'))
    df['time'] = df['datetime'].apply(lambda x: x.strftime('%H%M%S'))
    

    and then be specific about which columns to export to csv:

    df[['date', 'time', ... ]].to_csv('df.csv')
    

提交回复
热议问题