Created
July 20, 2020 16:58
-
-
Save sengstacken/4f0eab8d0d4796c226731eca3d4e35c0 to your computer and use it in GitHub Desktop.
one hot encoding - pandas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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