Skip to content

Instantly share code, notes, and snippets.

@mzaradzki
Created July 3, 2017 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzaradzki/275d718687040d36cc776351918dc3b3 to your computer and use it in GitHub Desktop.
Save mzaradzki/275d718687040d36cc776351918dc3b3 to your computer and use it in GitHub Desktop.
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