One Hot Encoding Python Pandas Code Example

Snippet 1

  y = pd.get_dummies(df.Countries, prefix='Country')
print(y.head())
# from here you can merge it onto your main DF 

Snippet 2

  from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
df = df.join(pd.DataFrame(mlb.fit_transform(df.pop('Col3')),
                          columns=mlb.classes_,
                          index=df.index)) 

Copyright © Code Fetcher 2020

 

 

Leave a comment

Your email address will not be published. Required fields are marked *