首页 > 解决方案 > 数据框中的对象值的 KeyError

问题描述

有人可以解释为什么“Louisville”返回 KeyError 吗?据我了解,它在数据框中。我错过了什么?

这是数据的样子。它是一个 CSV。

数据

off_data.head()看起来像

off_data.head()

off_data.index()

off_data.index()

off_data.columns

off_data.columns

off_data[0:2].to_dict()

off_data[0:2].to_dict()

Rajith Thennakoon 的建议

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 in get_loc(self, key, method, tolerance) 2896 try: -> 2897 return self._engine.get_loc (key) 2898 除了 KeyError:

pandas._libs.index.IndexEngine.get_loc() 中的 pandas_libs\index.pyx

pandas._libs.index.IndexEngine.get_loc() 中的 pandas_libs\index.pyx

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:“路易斯维尔”

在处理上述异常的过程中,又出现了一个异常:

4 5 类型(off_data) ----> 6 off_data.loc["Louisville",0] 中的 KeyError Traceback (最近一次调用最后一次)

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in 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 in _getitem_tuple(self, tup) 803 def _getitem_tuple(self, tup): 804 try: --> 805 return self._getitem_lowerdim(tup) 806 除了 IndexingError : 807 通行证

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_lowerdim(self, tup) 927 for i, key in enumerate(tup): 928 if is_label_like(key) or isinstance(key, tuple): - -> 929 section = self._getitem_axis(key, axis=i) 930 931 # 我们得到了一个标量?

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis) 1848 # 直接查找 1849 self._validate_key(key, axis) -> 1850 return self._get_label(键,轴=轴)1851 1852

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis) 158 raise IndexingError("这里没有切片,在别处处理") 159 --> 160 return self.obj._xs (标签,轴=轴)161 162 def _get_loc(自我,键:int,轴:int):

~\Anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level) 3735 loc, new_index = self.index.get_loc_level(key, drop_level=drop_level) 3736
else: -> 3737 loc = self.index.get_loc(key) 3738 3739 if isinstance(loc, np.ndarray):

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2897 return self._engine.get_loc(key) 2898 `

标签: pythonpandasdataframekeyerror

解决方案


您可以通过以下方式获取该行:

off_data.loc[off_data['Team'] == "Louisville"]

您进行定位的方式需要列名,根据您的输出显示为团队,您可以尝试这些以查看它们是否有效:

In [4496]: df2.loc[0,"Team"]                                                                                                                                                   
Out[4496]: 'Michigan'

In [4497]: df2.loc[1,"Team"]                                                                                                                                                   
Out[4497]: 'Louisville'

看起来数据中有空格,这是一种在末尾去除空格的快速方法:

off_data.index = off_data.index.str.strip()

这应该让您按原样进行搜索:

off_data[off_data.index == 'Louisville']

推荐阅读