刚开始学习python。我试图用这些代码打开两个文件,但得到错误 - 列表索引必须是整数或切片,而不是str
请帮忙。
import numpy as np
import pandas as pd
import datetime as dt
real_gdp = pd.read_csv('GDPC1.csv',index_col=['DATE'],parse_dates=['DATE'])
real_gdppot = pd.read_excel('GDPPOT.xls','GDPPOT',skiprows=13,index_col=['DATE'])
追踪(最近一次呼叫最后一次):
File "<ipython-input-5-dd98dfff0f6b>", line 14, in <module> index_col=['DATE'])
File "C:\Anaconda3\lib\site-packages\pandas\io\excel.py", line 170, in read_excel skip_footer=skip_footer, converters=converters, **kwds)
File "C:\Anaconda3\lib\site-packages\pandas\io\excel.py", line 418, in _parse_excel last = data[offset][col]
TypeError: list indices must be integers or slices, not str
答案 0 :(得分:1)
我想你以后可以set_index
:
real_gdppot = pd.read_excel('GDPPOT.xls','GDPPOT',skiprows=13,index_col=None)
real_gdppot = real_gdppot.set_index('DATE')