【发布时间】:2021-11-14 07:09:44
【问题描述】:
我想在人口普查数据集中执行一次性编码:
https://archive.ics.uci.edu/ml/datasets/census+income
我要执行的列在国家列中,所以我做了以下内容:
import pandas as pd
from sklearn import preprocessing
def abrirArchivo(fileR):
head=["gt lt 50","age","workclass","fnlwgt","edu","edu-num","mar-sta","occ","rela","race","sex","cap-gain","cap-loss","country","hpw"]
f=pd.read_csv(fileR,sep=',')
f.columns=head
ohe=oneHot(f)
print (ohe)
def oneHot(f):
f[["country"]]=pd.get_dummies(f[["country"]])
return f
但我收到一个错误提示:
ValueError: Columns must be same length as key
当我进行序数编码时,我对以下代码没有任何问题:
pp=preprocessing.OrdinalEncoder()
f[["country"]]=pp.fit_transform(f[["country"]])
我想要将转换后的 ohe(虚拟变量)连接到我原来的 panda 数据框,以便将其用于分类模型。
有什么帮助吗?
【问题讨论】:
-
f.join(pd.get_dummies(f["country"]))?