Categories
Python Answers

How to hot encode in Python Pandas?

Spread the love

Sometimes, we want to hot encode in Python Pandas.

In this article, we’ll look at how o hot encode in Python Pandas.

How to hot encode in Python Pandas?

To hot encode in Python Pandas, we an use the get_dummies method.

For instance, we write

import pandas as pd

s = pd.Series(list('abca'))
pd.get_dummies(s)

to create a series from the list with pd.Series.

Then we call get_dummies with series s to convert the categorical variable into dummy/indicator variables.

Conclusion

To hot encode in Python Pandas, we an use the get_dummies method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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