Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从数据帧创建csv文件后替换日期时间索引_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 3.x 从数据帧创建csv文件后替换日期时间索引

Python 3.x 从数据帧创建csv文件后替换日期时间索引,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,输出: data = pd.read_csv('data.csv') print(data) data.set_index('trade_date', inplace=True) print(data) data = pd.read_csv('data_reidx.csv') 我想将交易日期设置为索引 code trade_date open high low close volume 0 2GO 2012-06-04 1.750

输出:

data = pd.read_csv('data.csv')
print(data)
data.set_index('trade_date', inplace=True)
print(data)
data = pd.read_csv('data_reidx.csv')
我想将交易日期设置为索引

       code  trade_date   open   high    low  close       volume
0       2GO  2012-06-04  1.750  1.750  1.750  1.750       5000.0
1       2GO  2012-06-05  1.750  1.980  1.750  1.900       8000.0
2       2GO  2012-06-07  1.960  1.960  1.800  1.800       8000.0
3       2GO  2012-06-11  1.900  1.980  1.900  1.980      50000.0
4       2GO  2012-06-13  1.990  1.990  1.900  1.900      19000.0
输出:

data = pd.read_csv('data.csv')
print(data)
data.set_index('trade_date', inplace=True)
print(data)
data = pd.read_csv('data_reidx.csv')
。然后将其写入csv文件

           code   open   high    low  close       volume
trade_date
2012-06-04  2GO  1.750  1.750  1.750  1.750       5000.0
2012-06-05  2GO  1.750  1.980  1.750  1.900       8000.0
2012-06-07  2GO  1.960  1.960  1.800  1.800       8000.0
2012-06-11  2GO  1.900  1.980  1.900  1.980      50000.0
2012-06-13  2GO  1.990  1.990  1.900  1.900      19000.0
然而,当我再次阅读csv文件时,trade_date已恢复为一列,并被传统的索引所取代

data.to_csv('data_reidx.csv')
输出:

data = pd.read_csv('data.csv')
print(data)
data.set_index('trade_date', inplace=True)
print(data)
data = pd.read_csv('data_reidx.csv')

将数据帧写入csv时,如何保留日期时间索引?

加载时,请尝试告诉熊猫哪列是索引:

        trade_date code   open   high    low  close       volume
0       2012-06-04  2GO  1.750  1.750  1.750  1.750       5000.0
1       2012-06-05  2GO  1.750  1.980  1.750  1.900       8000.0
2       2012-06-07  2GO  1.960  1.960  1.800  1.800       8000.0
3       2012-06-11  2GO  1.900  1.980  1.900  1.980      50000.0
4       2012-06-13  2GO  1.990  1.990  1.900  1.900      19000.0
如果你需要更多帮助,这里有一个很好的指南,它比我解释得更好:

试着摆脱-

下次阅读时,请正常阅读-

data.to_csv('data_reidx.csv', index_label=False )
输出

data = pd.read_csv('data_reidx.csv')

你能试试data=pd.read\u csv('data\u reidx.csv',index\u col='trade\u date')吗。让我知道它是否有效。@itaybenhaim这是有效的。在
data.to\u csv('data\u reidx.csv',index\u label=False)
中,即使在正常读取csv时,日期索引也会保留,但“交易日期”列名称已消失。两者都很好。在我的情况下,列名并不重要。请随意投票:)@itaybenhaim在我到达15代表时就可以了:)