Label encoding across multiple columns in scikit-learn

后端 未结 22 2300
礼貌的吻别
礼貌的吻别 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:43

    If you have numerical and categorical both type of data in dataframe You can use : here X is my dataframe having categorical and numerical both variables

    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    
    for i in range(0,X.shape[1]):
        if X.dtypes[i]=='object':
            X[X.columns[i]] = le.fit_transform(X[X.columns[i]])
    

    Note: This technique is good if you are not interested in converting them back.

提交回复
热议问题