Skip to content

Instantly share code, notes, and snippets.

@sengstacken
Created July 20, 2020 16:58
Show Gist options
  • Save sengstacken/4f0eab8d0d4796c226731eca3d4e35c0 to your computer and use it in GitHub Desktop.
Save sengstacken/4f0eab8d0d4796c226731eca3d4e35c0 to your computer and use it in GitHub Desktop.
one hot encoding - pandas
import pandas as pd
# df now has two columns: name and country
df = pd.DataFrame({
'name': ['josef','michael','john','bawool','klaus'],
'country': ['russia', 'germany', 'australia','korea','germany']
})
# use pd.concat to join the new columns with your original dataframe
df = pd.concat([df,pd.get_dummies(df['country'], prefix='country')],axis=1)
# now drop the original 'country' column (you don't need it anymore)
df.drop(['country'],axis=1, inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment