【问题标题】:list indices must be integers or slices, not str列表索引必须是整数或切片,而不是 str
【发布时间】:2016-05-25 22:54:07
【问题描述】:

刚开始学习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']) 

Traceback(最近一次通话最后一次):

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

【问题讨论】:

  • 您是否费心查看read_csv 的帮助及其预期的参数类型?
  • 我想你可以试试real_gdppot = pd.read_excel('GDPPOT.xls','GDPPOT',skiprows=13,index_col=None) 然后real_gdppot = real_gdppot.set_index('DATE')
  • @jezrael 这行得通,非常感谢。

标签: python pandas


【解决方案1】:

我想你以后可以set_index:

real_gdppot = pd.read_excel('GDPPOT.xls','GDPPOT',skiprows=13,index_col=None)

real_gdppot = real_gdppot.set_index('DATE')

【讨论】:

    猜你喜欢
    • 2017-10-09
    • 2015-12-09
    • 2021-09-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多