python keyerror not in index,Pandas 0.20.3在尝试选择要在DataFram中显示的列时,KeyError:'[1 2]not in index'”...

我正在阅读《掌握熊猫理财》一书,并为标准普尔500指数创建了以下数据框架:sp500 = pd.read_csv("sp500.csv", index='Symbol', usecols=[0, 2, 3, 7])

返回以下数据帧:

练习的一部分是使用以下代码显示只包含选择列的数据帧:

^{pr2}$

应生成以下数据帧:

运行代码时,出现以下错误:KeyError: '[1 2] not in index'

回溯---------------------------------------------------------------------------

KeyError Traceback (most recent call last)

in ()

----> 1 sp500[[1, 2]].head(3)

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)

1956 if isinstance(key, (Series, np.ndarray, Index, list)):

1957 # either boolean or fancy integer index

-> 1958 return self._getitem_array(key)

1959 elif isinstance(key, DataFrame):

1960 return self._getitem_frame(key)

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.p

你可能感兴趣的:(python,keyerror,not,in,index)