【发布时间】:2021-10-04 03:06:32
【问题描述】:
在 jupyter notebook 中运行的代码
import pandas as pd
import numpy as np
myindex = ['USA','Canada','Mexico']
mydata = [1776,1867,1821]
myser = pd.Series(data=mydata,index=myindex)
myser['USA']
代码图片
即使我输入了正确的索引,我也会在此处收到一个关键错误(带有以下错误消息)
KeyError Traceback (most recent call last)
<ipython-input-41-422adf17bf03> in <module>
----> 1 myser['USA']
D:\Anaconda\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
851
852 elif key_is_scalar:
--> 853 return self._get_value(key)
854
855 if is_hashable(key):
D:\Anaconda\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
959
960 # Similar to Index.get_value, but we do not fall back to positional
--> 961 loc = self.index.get_loc(label)
962 return self.index._get_values_for_loc(self, loc, label)
963
D:\Anaconda\lib\site-packages\pandas\core\indexes\range.py in get_loc(self, key, method, tolerance)
352 except ValueError as err:
353 raise KeyError(key) from err
--> 354 raise KeyError(key)
355 return super().get_loc(key, method=method, tolerance=tolerance)
356
KeyError: 'USA'
【问题讨论】:
-
请不要将您的代码作为图片发布。
-
我投了反对票,因为Code is an image
-
我也添加了代码
-
在您的笔记本(或新笔记本)中打开一个新单元格,复制粘贴您的代码。你还有错误吗?
标签: python pandas jupyter-notebook series keyerror