The default output format of to_csv()
is:
to_csv()的默认输出格式为:
12/14/2012 12:00:00 AM
I cannot figure out how to output only the date part with specific format:
我无法弄清楚如何只输出具有特定格式的日期部分:
20121214
or date and time in two separate columns in the csv file:
或csv文件中两个单独列中的日期和时间:
20121214, 084530
The documentation is too brief to give me any clue as to how to do these. Can anyone help?
文档太简短了,不能给我任何关于如何做这些的线索。有人可以帮忙吗?
2 个解决方案
#1
31
You could use strftime
to save these as separate columns:
您可以使用strftime将这些保存为单独的列:
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:
然后具体说明要导出到csv的列:
df[['date', 'time', ... ]].to_csv('df.csv')
#2
65
With the new version of Pandas you can use the date_format parameter of the to_csv method:
使用新版本的Pandas,您可以使用to_csv方法的date_format参数:
df.to_csv(filename, date_format='%Y%m%d')
#1
31
You could use strftime
to save these as separate columns:
您可以使用strftime将这些保存为单独的列:
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:
然后具体说明要导出到csv的列:
df[['date', 'time', ... ]].to_csv('df.csv')
#2
65
With the new version of Pandas you can use the date_format parameter of the to_csv method:
使用新版本的Pandas,您可以使用to_csv方法的date_format参数:
df.to_csv(filename, date_format='%Y%m%d')