数据框中对象值的 KeyError
KeyError for an object value that is in the dataframe
有人可以解释为什么 '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 (most recent call
last) ~\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 except 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 in
pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas_libs\hashtable_class_helper.pxi in
pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Louisville'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call
last) in
4
5 type(off_data)
----> 6 off_data.loc["Louisville",0]
~\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 # we by definition only have the 0th axis
~\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 except IndexingError:
807 pass
~\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 # we have yielded a scalar ?
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in
_getitem_axis(self, key, axis) 1848 # fall thru to straight lookup 1849 self._validate_key(key, axis)
-> 1850 return self._get_label(key, axis=axis) 1851 1852
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in
_get_label(self, label, axis)
158 raise IndexingError("no slices here, handle elsewhere")
159
--> 160 return self.obj._xs(label, axis=axis)
161
162 def _get_loc(self, key: int, axis: 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 `
您可以通过以下方式获取该行:
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']
试试这个
off_data.index = off_data.index.str.strip()
off_data.loc[off_data.index == "Louisville"]
编辑
如果阅读dataframe.you时需要去除空格可以使用skipinitialspace=True。这将跳过分隔符后的空格。
df1 = pd.read_csv(.. skipinitialspace=True)
或者需要删除特定列的空格,你可以这样使用
df["column_name"] = df["column_name"].str.strip()
或者您也可以使用 pandas rstrip or lstrip。
lstrip,从左侧 Series/Index 中的每个字符串中去除空格(包括换行符)或一组指定字符。
rstrip,从右侧 Series/Index 中的每个字符串中去除空格(包括换行符)或一组指定字符。
有人可以解释为什么 '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 (most recent call last) ~\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 except 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 in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Louisville'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last) in 4 5 type(off_data) ----> 6 off_data.loc["Louisville",0]
~\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 # we by definition only have the 0th axis
~\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 except IndexingError: 807 pass
~\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 # we have yielded a scalar ?
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis) 1848 # fall thru to straight lookup 1849 self._validate_key(key, axis) -> 1850 return self._get_label(key, axis=axis) 1851 1852
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis) 158 raise IndexingError("no slices here, handle elsewhere") 159 --> 160 return self.obj._xs(label, axis=axis) 161 162 def _get_loc(self, key: int, axis: 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 `
您可以通过以下方式获取该行:
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']
试试这个
off_data.index = off_data.index.str.strip()
off_data.loc[off_data.index == "Louisville"]
编辑
如果阅读dataframe.you时需要去除空格可以使用skipinitialspace=True。这将跳过分隔符后的空格。
df1 = pd.read_csv(.. skipinitialspace=True)
或者需要删除特定列的空格,你可以这样使用
df["column_name"] = df["column_name"].str.strip()
或者您也可以使用 pandas rstrip or lstrip。
lstrip,从左侧 Series/Index 中的每个字符串中去除空格(包括换行符)或一组指定字符。
rstrip,从右侧 Series/Index 中的每个字符串中去除空格(包括换行符)或一组指定字符。