python2.7:dataframe date_format =“%Y-%m-%d"不起作用 [英] python2.7: dataframe date_format="%Y-%m-%d" doesn't work

查看:546
本文介绍了python2.7:dataframe date_format =“%Y-%m-%d"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要以下数据框,另存为csv文件,并且日期时间索引格式为 %Y-%m-%d".

I want the dataframe as following, saved as csv file, and datetime index format is "%Y-%m-%d".

    date       price_am   price_pm
2017-06-01       E      E
2017-06-02       D      E
2017-06-03       C       D

我使用了以下代码,但它不起作用:

I used the following code, and it doesn't work:

df.to_csv('move.csv', date_format='%Y-%m-%d')

但是当我打开保存的cvs文件时,日期时间格式如下所示:

But when I opened saved cvs file, the datetime format is shown as following:

     date         price_am    price_pm
    2017/6/1         E      E
    2017/6/2         D      E
    2017/6/3         C       D

如何更改csv文件中的数据时间格式索引.谢谢!

How to change datatime format index in the csv file. Thanks!

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2557 entries, 2011-01-01 to 2017-12-31
Data columns (total 2 columns):
price_am    2527 non-null object
price_pm    2526 non-null object
dtypes: object(2)
memory usage: 59.9+ KB

推荐答案

您似乎在Excel中验证数据-原始日期已更改.

It looks like you verify data in Excel - original dates are changed.

尝试使用某些文本编辑器,例如Notepad++-csv文件数据正确.

Try some text editor like Notepad++ - csv file data are correct.

如果使用默认选项:

print (df.head().to_csv())
date,price_am,price_pm
2017-06-01,E,E
2017-06-02,D,E
2017-06-03,C,D

这篇关于python2.7:dataframe date_format =“%Y-%m-%d"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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