Traceback (most recent call last): File "C:\Users\木南\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\core\indexes\base.py", line 3652, in get_loc return self._engine.get_loc(casted_key) File "pandas\_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'income' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\木南\PycharmProjects\pythonProject1\venv\test.py", line 18, in <module> data['income'] = data['income'].apply(lambda x: 1 if x == '>50K' else 0) File "C:\Users\木南\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\core\frame.py", line 3761, in __getitem__ indexer = self.columns.get_loc(key) File "C:\Users\木南\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\core\indexes\base.py", line 3654, in get_loc raise KeyError(key) from err KeyError: 'income' Process finished with exit code 1
时间: 2023-07-20 12:26:46 浏览: 188
traceback-path-mapper:PyCharm无法处理来自远程服务器的回溯。 这是一个简单的脚本,用于修补Python回溯,将远程路径替换为本地路径
5星 · 资源好评率100%
这个错误是由于数据集中不存在名为 `income` 的列引起的。在第14行中,代码使用 `data.drop('income', axis=1)` 删除了目标变量 `income` 后的数据集作为训练集和测试集的特征集,但是在第18行中,代码又尝试去访问 `data['income']` 进行二元编码,从而引发了 KeyError 错误。建议在进行数据集划分前,先检查数据集中是否存在目标变量 `income`,例如使用 `print(data.columns)` 查看数据集中包含哪些列。
阅读全文