使用pandas.to_csv时如何指定日期格式? [英] How to specify date format when using pandas.to_csv?

查看:1212
本文介绍了使用pandas.to_csv时如何指定日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

to_csv()的默认输出格式为:

12/14/2012  12:00:00 AM

我无法弄清楚如何仅输出具有特定格式的日期部分:

I cannot figure out how to output only the date part with specific format:

20121214

或csv文件中两个单独列中的日期和时间:

or date and time in two separate columns in the csv file:

20121214,  084530

文档太简短,无法给我任何有关如何执行这些操作的线索.有人可以帮忙吗?

The documentation is too brief to give me any clue as to how to do these. Can anyone help?

推荐答案

您可以使用

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'))

,然后具体说明要导出到csv的列:

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

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

这篇关于使用pandas.to_csv时如何指定日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆