Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Last active February 21, 2019 08:15
Show Gist options
  • Select an option

  • Save hellpanderrr/a35be9ef3a5582a354d8 to your computer and use it in GitHub Desktop.

Select an option

Save hellpanderrr/a35be9ef3a5582a354d8 to your computer and use it in GitHub Desktop.
One hot encode a dataframe in pandas and sklearn
from sklearn.feature_extraction import DictVectorizer as DV
vectorizer = DV(sparse = False)
v = vectorizer.fit_transform(df.T.to_dict().values())
new_df = pd.DataFrame(v, columns=vectorizer.feature_names_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment