【发布时间】:2020-03-21 09:45:48
【问题描述】:
有人能解释一下为什么“Louisville”会返回 KeyError 吗?据我了解,它在数据框中。我错过了什么?
这是数据的样子。这是一个 CSV。
这就是off_data.head() 的样子
off_data.index()
off_data.columns
off_data[0:2].to_dict()
Rajith Thennakoon 的建议
{'Conf': {'Michigan St. ': 'B10', 'Louisville ': 'ACC'},
'AdjTempo': {'Michigan St. ': 70.4, 'Louisville ': 67.8},
'AdjOE': {'Michigan St. ': 114.4, 'Louisville ': 113.9},
'eFG%': {'Michigan St. ': 52.9, 'Louisville ': 60.7},
'TO%': {'Michigan St. ': 15.9, 'Louisville ': 17.1},
'OR%': {'Michigan St. ': 37.1, 'Louisville ': 32.8},
'FTRate': {'Michigan St. ': 30.9, 'Louisville ': 32.5},
'AdjDE': {'Michigan St. ': 85.1, 'Louisville ': 87.5},
'deFG%': {'Michigan St. ': 40.3, 'Louisville ': 42.9},
'dTO%': {'Michigan St. ': 20.7, 'Louisville ': 15.9},
'dOR%': {'Michigan St. ': 25.0, 'Louisville ': 27.6},
'dFTRate': {'Michigan St. ': 27.3, 'Louisville ': 26.0}}
输入
import pandas as pd
off_data = pd.read_csv(r'C:\Users\westc\Desktop\sports.data\ncaab\kenpomdata\off20.csv', index_col= 'Team')
type(off_data)
off_data.loc["Louisville",0]
输出
KeyError Traceback(最近调用 最后)~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py 在 get_loc(self, key, method, tolerance) 2896 尝试: -> 2897 return self._engine.get_loc(key) 2898 除了 KeyError:
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libs\hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas_libs\hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: '路易斯维尔'
在处理上述异常的过程中,又发生了一个异常:
KeyError Traceback(最近调用 最后)在 4 5 类型(off_data) ----> 6 off_data.loc["Louisville",0]
~\Anaconda3\lib\site-packages\pandas\core\indexing.py 在 getitem(self, key) 1416 except (KeyError, IndexError, AttributeError): 1417 pass -> 1418 return self._getitem_tuple(key) 1419 else: 1420 # 根据定义,我们只有第 0 个轴
~\Anaconda3\lib\site-packages\pandas\core\indexing.py 在 _getitem_tuple(自我,tup) 第803章 804尝试: --> 805 返回 self._getitem_lowerdim(tup) 806 除了索引错误: 807通过
~\Anaconda3\lib\site-packages\pandas\core\indexing.py 在 _getitem_lowerdim(自我,tup) 927 for i, 键入 enumerate(tup): 928 if is_label_like(key) 或 isinstance(key, tuple): --> 929 部分 = self._getitem_axis(key, axis=i) 930 第931章
~\Anaconda3\lib\site-packages\pandas\core\indexing.py 在 _getitem_axis(self, key, axis) 1848 # 直接查找 1849 self._validate_key(key, axis) -> 1850 返回 self._get_label(key, axis=axis) 1851 1852
~\Anaconda3\lib\site-packages\pandas\core\indexing.py 在 _get_label(自我,标签,轴) 158 raise IndexingError("这里没有切片,在别处处理") 159 --> 160 返回 self.obj._xs(label, axis=axis) 161 162 def _get_loc(自我,键:int,轴:int):
~\Anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, 轴,水平,drop_level)3735 loc,new_index = self.index.get_loc_level(key, drop_level=drop_level) 3736
别的: -> 3737 loc = self.index.get_loc(key) 3738 3739 if isinstance(loc, np.ndarray):~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py 在 get_loc(self, key, method, tolerance) 2897 返回 self._engine.get_loc(key) 2898 `
【问题讨论】:
-
你能清理错误信息,只显示 KeyError 和其他“错误”类型吗?
-
试试这个
off_data.loc[0,"Louisville"] -
0应该代表什么?
-
@rajiththennakoon 如果他们将“Teams”作为索引,那么这没有任何意义
-
向我们展示 df 读入后的样子,以及 off_data.index 和 off_data.columns
标签: python pandas dataframe keyerror