python - 如何修复熊猫数据框中的“键值错误”?
问题描述
所以我正在尝试制作一个机器学习模型来识别手势。我对机器学习和 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
我已经发布了完整的引用消息帮助将不胜感激。
解决方案
很可能您的火车 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 中删除。可能这正是你想要的,但我认为你应该知道。希望这有帮助。
推荐阅读
- flutter - 在颤动中居中一列容器,crossAxis不起作用
- typescript - 如何检查一个对象是否至少有一条记录?
- reactjs - 如何在反应中编写多语言代码?
- .htaccess - HtAccess:如果访问任何子域(不存在),则重定向到主域
- encoding - 通过 Foundations 发送的电子邮件显示 ?? 间距之间
- javascript - 如何使不和谐的 js 机器人在某个时间在不和谐的确切文本通道中发送随机消息(我制作一个列表并发送它)
- goland - 如何在 GoLand 终端中输入
- java - Maven 缩短执行中的类路径
- r - 选择器输入作为过滤器使用shinyJS禁用所有未禁用按钮
- javascript - 滚动类在页面加载时立即应用,而不是当用户在 iphone 上滚动时