简体   繁体   English

使用pandas.to_csv到YYYY-MM-DD时如何指定日期格式?

[英]How to specify date format when using pandas.to_csv to YYYY-MM-DD?

The OP of this post highlighted the lack of documentation for using the date_format argument in pandas. 这篇文章的OP强调了在pandas中使用date_format参数缺乏文档。 I would like to format the data into YYYY-MM-DD, not MM/DD/YYYY when using the to_csv command. 我想在使用to_csv命令时将数据格式化为YYYY-MM-DD,而不是MM / DD / YYYY。

I've tried: frame.to_csv(output_dir, index=False, encoding='utf-8', date_format='%Y-%m-%d') and it is not working, where frame is a DataFrame 我试过: frame.to_csv(output_dir, index=False, encoding='utf-8', date_format='%Y-%m-%d')并且它不起作用,其中frame是DataFrame

The file I'm trying to convert can be found here 我正在尝试转换的文件可以在这里找到

This is already supported in to_csv as a param date_format : to_csv已经支持这作为param date_format

t="""date,val
12/05/1984,sadas
1/11/1980,sadas
2/4/1945,sadas
22/10/1921,sadas"""
df = pd.read_csv(io.StringIO(t), parse_dates=[0])
df.to_csv(r'c:\data\date.csv', date_format='%Y-%m-%d')

Yields: 产量:

,date,val
0,1984-12-05,sadas
1,1980-01-11,sadas
2,1945-02-04,sadas
3,1921-10-22,sadas

The format strings are of the form taken from strftime 格式字符串的格式取自strftime

You can use the standard datetime string formatting behavior, here that would be: df.to_csv('df.csv, date_format="%Y-%m-%d") https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior 您可以使用标准日期时间字符串格式化行为,此处可能是:df.to_csv('df.csv,date_format =“%Y-%m-%d”) https://docs.python.org/2/library /datetime.html#strftime-strptime-behavior

EDIT: don't iterate through the dataframe with strftime, that's highly unoptimized and theres already a built-in function for performing the operation you want 编辑:不要使用strftime迭代数据帧,这是非常不优化的,并且已经有一个内置函数来执行你想要的操作

您将显示迭代csv'行并获取每个日期 ,并使用日期时间的 strptime方法对其进行格式化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 pandas.to_csv 时如何指定日期格式? - How to specify date format when using pandas.to_csv? 如何使用 pandas 在 Python 中识别日期列并将其格式化为 YYYY-MM-DD? - How can I identify date columns and format it to YYYY-MM-DD in Python using pandas? 如何将不同格式的字符串(YYYY-MM-DD,DD-MM-YYYY)转换为熊猫中一种格式的日期对象? - How to convert string of different format(YYYY-MM-DD, DD-MM-YYYY) to date object of one format in pandas? 将日期格式为“ mm / dd / yyyy”的Pandas数据框中的所有行值更改为“ yyyy-mm-dd” - Change all row values in Pandas dataframe in date format “mm/dd/yyyy” to “yyyy-mm-dd” 检查日期格式 YYYY-MM-DD - Checking a date format YYYY-MM-DD 如何在 Pandas 中创建具有 ```yyyy-mm-dd``` 格式的虚拟列? - How to create dummy column with ```yyyy-mm-dd``` format in Pandas? 如何将混合日期格式mm-dd-yyyy和yyyy-mm-dd转换为熊猫中的日期 - how to convert mix date formats mm-dd-yyyy and yyyy-mm-dd to date in pandas 如何在 Python 中将日期格式 (YYYY-MM-DD) 转换为 (YYYY,MM,DD) - How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python 使用 python 将日期格式 mm/dd/yyyy hh:mm:ss AM 更改为 yyyy-mm-dd hh:mm:ss 格式 - change date format mm/dd/yyyy hh:mm:ss AM to yyyy-mm-dd hh:mm:ss format using python 在2014年1月4日给出时如何将mm / dd / yyyy格式转换为python 3.0中的yyyy-mm-dd - How to convert from mm/dd/yyyy format to yyyy-mm-dd in python 3.0 when given 1/4/2014
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM