Python KeyError回溯(上次调用)~\anaconda3\lib\site packages\pandas\core\index\base.py在get\u loc中(self、key、method、tolerance)

Python KeyError回溯(上次调用)~\anaconda3\lib\site packages\pandas\core\index\base.py在get\u loc中(self、key、method、tolerance),python,python-3.x,pandas,Python,Python 3.x,Pandas,我在试kaggle的房价数据。我试图从名为“Alley”的列中清除NaN值 for column in missing_data.columns.values.tolist(): print(column) print(missing_data[column].value_counts()) print("") 这些是我在运行上述代码时得到的一些值。我用它的平均值替换了LotFrontage的NaN值,并想用它的频率替换“Alley”中的NaN值。

我在试kaggle的房价数据。我试图从名为“Alley”的列中清除NaN值

for column in missing_data.columns.values.tolist():
    print(column)
    print(missing_data[column].value_counts())
    print("")
这些是我在运行上述代码时得到的一些值。我用它的平均值替换了LotFrontage的NaN值,并想用它的频率替换“Alley”中的NaN值。 但是当我写这段代码时,我得到了一个错误

train['Alley'].value_counts()
keyrerror回溯(最近一次调用)
get\u loc中的~\anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2645请尝试:
->2646返回自引擎。获取位置(钥匙)
2647键错误除外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:“胡同”
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在里面
1#用频率替换Alley中的nan值
---->2列火车[‘巷’]。价值(单位计数)
~\anaconda3\lib\site packages\pandas\core\frame.py in\uuuu\getitem\uuuuuuu(self,key)
2798如果self.columns.nlevels>1:
2799返回自我。\u获取项目\u多级(键)
->2800索引器=self.columns.get_loc(键)
2801如果是_整数(索引器):
2802索引器=[索引器]
get\u loc中的~\anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2646返回自引擎。获取位置(钥匙)
2647键错误除外:
->2648返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2649 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
2650如果indexer.ndim>1或indexer.size>1:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:“胡同”

为什么我会犯这个错误?当名为Alley的列存在时?

似乎您可能为数据帧分配了不同的值,因此在下一次迭代中,它将抛出一个错误,因为数据帧可能已分配给panda series

train['Alley'].value_counts()
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             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: 'Alley'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-72-8e1e57b44782> in <module>
      1 #replace nan values in Alley with frequency
----> 2 train['Alley'].value_counts()

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

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: 'Alley'