我有一个由数据框加载的数据集,其中类标签需要使用来自 scikit-learn 的 LabelEncoder
进行编码。列 label
是具有以下类的类标签列:
[‘Standing’, ‘Walking’, ‘Running’, ‘null’]
要执行标签编码,我尝试了以下但它不起作用。我该如何解决?
from sklearn import preprocessing
import pandas as pd
df = pd.read_csv('dataset.csv', sep=',')
df.apply(preprocessing.LabelEncoder().fit_transform(df['label']))
原文由 Kristofer 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以尝试如下操作:
或者以下也可以工作:
它将用编码标签替换数据框中的原始值
label
值。