Sklearn Label Encoding multiple columns pandas dataframe

后端 未结 5 1070
春和景丽
春和景丽 2020-12-10 03:25

I try to encode a number of columns containing categorical data (\"Yes\" and \"No\") in a large pandas dataframe. The complete dataframe contains

5条回答
  •  心在旅途
    2020-12-10 04:18

    First, find out all the features with type object:

    objList = all_data.select_dtypes(include = "object").columns
    print (objList)
    

    Now, to convert the above objList features into numeric type, you can use a forloop as given below:

    #Label Encoding for object to numeric conversion
    from sklearn.preprocessing import LabelEncoder
    le = LabelEncoder()
    
    for feat in objList:
        df[feat] = le.fit_transform(df[feat].astype(str))
    
    print (df.info())
    

    Note that we are explicitly mentioning as type string in the forloop because if you remove that it throws an error.

提交回复
热议问题