![](/img/trans.png)
[英]KeyError: 'class_name' in python3.7/site-packages/pandas/core/indexes/base.py
[英]KeyError Traceback (most recent call last) ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
我正在尝试来自 kaggle 的房价数据集。 我试图清除名为“Alley”的列中的 NaN 值
for column in missing_data.columns.values.tolist():
print(column)
print(missing_data[column].value_counts())
print("")
Id
False 1460
Name: Id, dtype: int64
MSSubClass
False 1460
Name: MSSubClass, dtype: int64
MSZoning
False 1460
Name: MSZoning, dtype: int64
LotFrontage
False 1201
True 259
Name: LotFrontage, dtype: int64
LotArea
False 1460
Name: LotArea, dtype: int64
Street
False 1460
Name: Street, dtype: int64
Alley
True 1369
False 91
Name: Alley, dtype: int64
LotShape
False 1460
Name: LotShape, dtype: int64
LandContour
False 1460
Name: LandContour, dtype: int64
Utilities
False 1460
Name: Utilities, dtype: int64
LotConfig
False 1460
Name: LotConfig, dtype: int64
这些是我在运行上述代码时得到的一些值。 我用它的平均值替换了 LotFrontage 的 NaN 值,并想用它的频率替换“Alley”中的 NaN 值。 但是当我写这段代码时,我得到了一个错误。
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'
为什么我收到这个错误? 当名为 Alley 的列存在时?
似乎您可能为数据帧分配了不同的值,因此在下一次迭代中它会抛出错误,因为数据帧可能已分配给熊猫系列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.