标签编码器未将str转换为int

时间:2019-11-08 05:41:18

标签: python-3.x scikit-learn data-science

col = ['date','age','race', 'sex','procedure1', 'procedure2', 'procedure3', 'a_diag', 'p_diag','s_diag1','s_diag2', 's_diag3', 's_diag4', 's_diag5']

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for i in col:
    df[i] = le.fit_transform(df[i].astype('object'))

对于上面的代码,出现以下错误:

'<' not supported between instances of 'str' and 'int'

1 个答案:

答案 0 :(得分:0)

我建议您跑步:

df.dtypes

首先,要检查哪些列已经是数字列,输出应如下所示:

date object
age int32

我将从您的循环中排除所有数字列。然后将循环代码更改为:

 df[i] = le.fit_transform(df[i].astype('str'))

如果仍然有错误,请检查以下内容:TypeError: '<=' not supported between instances of 'str' and 'int'