Pandas将数据转化为one-hot形式

def preprocessing(dfdata):
    dfresult= pd.DataFrame()
    #Pclass
    dfPclass = pd.get_dummies(dfdata['Pclass'])
    dfPclass.columns = ['Pclass_' +str(x) for x in dfPclass.columns ]
    dfresult = pd.concat([dfresult,dfPclass],axis = 1)

    return(dfresult)

Pclass原来的取值是1,2,3,转换后变成了Pclass_1,Pclass_2,Pclass_3三个one-hot形式的列。

参考源:https://github.com/lyhue1991/eat_tensorflow2_in_30_days

你可能感兴趣的:(编程基础)