首页 > 解决方案 > 我正在使用 LabelEncoder 在我的数据框中标记分类数据。我收到此错误。'(slice(None, None, None), 2)' 是无效键

问题描述

我有一个分类数据,我正在尝试使用 LabelEncoder 对数据进行编码以运行回归模型。但是,我收到此错误。

'(slice(None, None, None), 2)' 是无效键

我正在尝试对数据框中的第三列进行编码。

from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
df[:, 2] = labelencoder.fit_transform(df[:, 2])

假设第 3 列的数据如下:

Col3
A
B
D
D
A
B
B

预期结果是:

Col3
1
2
3
3
1
2
2

标签: python-3.xpandas

解决方案


推荐阅读