gpt4 book ai didi

python - 如何将虚拟列连接到主表?

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:29 25 4
gpt4 key购买 nike

我正在尝试为分类变量创建虚拟变量。但是,当我创建它们时,我收到“ValueError:列重叠但未指定后缀”。这是代码:

dummy2 = pd.get_dummies(data['Teaching'], prefix='Teach')

dummy2.head ()
dummy2.columns = ['Small/Rural','Teaching']

data = data.join(dummy2)
##################
dummy3 = pd.get_dummies(data['Gender'], prefix='Gender_')

dummy3.head()
dummy3.columns = ['Male','Female']

data = data.join(dummy3)
#####################
dummy4 = pd.get_dummies(data['PositionTitle'], prefix='pos_')

dummy4.head()
dummy4.columns = ['Acting Director','RegioReresentative']

data = data.join(dummy4)
#####################


dummy5 = pd.get_dummies(data['Compensation'], prefix='COMP')

dummy5.head()
dummy5.columns = ['23987','46978','89473','248904']

data = data.join(dummy5)

#################3
dummy6 = pd.get_dummies(data['TypeControl'], prefix='Type')

dummy6.head()
dummy6.columns = ['City/country','District','Investor','Non Profit']

data = data.join(dummy6)

最佳答案

关于如何使用 pd.concat 执行此操作有一个很好的解释,位于 https://towardsdatascience.com/the-dummys-guide-to-creating-dummy-variables-f21faddb1d40 。对此示例进行修改将如下所示:

dummy2 = pd.get_dummies(data['Teaching'], prefix='Teach')
data = pd.concat([data, dummy2], axis = 1)

关于python - 如何将虚拟列连接到主表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com