Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dfOHE = None
for col in categories: # encode 1 category at a time
one_hot = pd.get_dummies(df[col], prefix=col)
# drop column as it is now encoded
if dfOHE is None:
dfOHE = df.drop(col, axis=1)
else:
dfOHE = dfOHE.drop(col, axis=1)
# join the encoded dataframe
dfOHE = dfOHE.join(one_hot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment