how to use one hot encoding in multiple columns at once

Solutions on MaxInterview for how to use one hot encoding in multiple columns at once by the best coders in the world

showing results for - "how to use one hot encoding in multiple columns at once"
Julian
06 Mar 2017
1categorical_cols = ['a', 'b', 'c', 'd'] 
2
3from sklearn.preprocessing import LabelEncoder
4# instantiate labelencoder object
5le = LabelEncoder()
6
7# apply le on categorical feature columns
8data[categorical_cols] = data[categorical_cols].apply(lambda col: le.fit_transform(col))    
9from sklearn.preprocessing import OneHotEncoder
10ohe = OneHotEncoder()
11
12#One-hot-encode the categorical columns.
13#Unfortunately outputs an array instead of dataframe.
14array_hot_encoded = ohe.fit_transform(data[categorical_cols])
15
16#Convert it to df
17data_hot_encoded = pd.DataFrame(array_hot_encoded, index=data.index)
18
19#Extract only the columns that didnt need to be encoded
20data_other_cols = data.drop(columns=categorical_cols)
21
22#Concatenate the two dataframes : 
23data_out = pd.concat([data_hot_encoded, data_other_cols], axis=1)
24