Label encoding across multiple columns in scikit-learn

后端 未结 22 1950
礼貌的吻别
礼貌的吻别 2020-11-22 09:02

I\'m trying to use scikit-learn\'s LabelEncoder to encode a pandas DataFrame of string labels. As the dataframe has many (50+) columns, I want to a

22条回答
  •  名媛妹妹
    2020-11-22 09:27

    If you have all the features of type object then the first answer written above works well https://stackoverflow.com/a/31939145/5840973.

    But, Suppose when we have mixed type columns. Then we can fetch the list of features names of type object type programmatically and then Label Encode them.

    #Fetch features of type Object
    objFeatures = dataframe.select_dtypes(include="object").columns
    
    #Iterate a loop for features of type object
    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    
    for feat in objFeatures:
        dataframe[feat] = le.fit_transform(dataframe[feat].astype(str))
     
    
    dataframe.info()
    

提交回复
热议问题