I have the following code to one-hot-encode 2 columns I have.
# encode city labels using one-hot encoding scheme
city_ohe = OneHotEncoder(categories=\'auto\'
Why don't you take a look at pd.get_dummies? Here's how you can encode:
df['city'] = df['city'].astype('category')
df['phone'] = df['phone'].astype('category')
df = pd.get_dummies(df)
You you are almost there... Like you said you can add all the columns you want to encode in fit_transform
directly.
ohe = OneHotEncoder(categories='auto')
feature_arr = ohe.fit_transform(df[['phone','city']]).toarray()
feature_labels = ohe.categories_
And then you just need to do the following:
feature_labels = np.array(feature_labels).ravel()
Which enables you to name your columns like you wanted:
features = pd.DataFrame(feature_arr, columns=feature_labels)