I try to encode a number of columns containing categorical data (\"Yes\" and \"No\") in a large pandas dataframe. The complete dataframe contains
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.