Introduction

In this tutorial, we want to one-hot encode a categorical variable of a Pandas DataFrame. In order to do this, we use the get_dummies() function of Pandas.

Import Libraries

First, we import the following python modules:

import pandas as pd

Create Pandas DataFrame

Next, we create a Pandas DataFrame with some example data from a dictionary:

data = {
    "language": ["Python", "Python", "Java", "JavaScript"],
    "framework": ["Django", "FastAPI", "Spring", "ReactJS"],
    "users": [20000, 9000, 7000, 5000]
}
df = pd.DataFrame(data)
df

Create Dummy Variables

Now, we would like to one-hot encode the column "language" of the Pandas DataFrame.

To do this, we convert the column "language" into dummy variables by using the get_dummies() function of Pandas:

df = pd.get_dummies(df, columns=['language'])
df

Conclusion

Congratulations! Now you are one step closer to become an AI Expert. You have seen that it is very easy to one-hot encode a categorical variable of a Pandas DataFrame. We can simply use the get_dummies() function of Pandas. Try it yourself!