LabelEncoder: TypeError: '>' not supported between instances of 'float' and 'str'

前端 未结 3 1434
一生所求
一生所求 2021-01-30 00:25

I\'m facing this error for multiple variables even treating missing values. For example:

le = preprocessing.LabelEncoder()
categorical = list(df.select_dtypes(in         


        
3条回答
  •  忘掉有多难
    2021-01-30 00:50

    This is due to the series df[cat] containing elements that have varying data types e.g.(strings and/or floats). This could be due to the way the data is read, i.e. numbers are read as float and text as strings or the datatype was float and changed after the fillna operation.

    In other words

    pandas data type 'Object' indicates mixed types rather than str type

    so using the following line:

    df[cat] = le.fit_transform(df[cat].astype(str))
    


    should help

提交回复
热议问题