首页 > 解决方案 > 如何修复熊猫数据框中的“键值错误”?

问题描述

所以我正在尝试制作一个机器学习模型来识别手势。我对机器学习和 numpy 很陌生。我面临以下错误

pixel0  pixel1  pixel2  pixel3  ...  pixel9212  pixel9213  pixel9214  

pixel9215
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255

[5 rows x 9216 columns]
Traceback (most recent call last):
  File "classification.py", line 12, in <module>
    y = np.array(train.pop('label'))
  File "/home/bing/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 809, in pop
    result = self[item]
  File "/home/bing/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/bing/.local/lib/python2.7/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'label'

这是我的分类.py

 import pandas as pd
 import numpy as np

 train = pd.read_csv("train60.csv")
 print(train.head())
 y = np.array(train.pop('label'))  # this is the error

我已经发布了完整的引用消息帮助将不胜感激。

标签: pythonpandasnumpy

解决方案


很可能您的火车 df 没有名为“标签”的列。您可以通过对脚本的简单添加来检查它。

if 'label' in train.columns:
    print("label column is present")
else:
    print("label column is not here. Popping 'label' will produce KeyError")

当您使用 pop 功能时,您还想从火车 df 中删除“标签”吗?我这样说是因为 df.pop('MyColumnName') 将返回 'MyColumnName' 并将其从 df 中删除。可能这正是你想要的,但我认为你应该知道。希望这有帮助。


推荐阅读