How to transform categorical text variables into integers using Pandas

1 min

This process is a way to transform your category into an integer that can be used as a reference in some kind of algorithm.

Text into integers

It is extremely useful when you want to feed this data into a machine learning algorithm. Because algorithms usually prefer numbers since it is easier to digest and comprehend.

Here is how to do it

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

# We transform text categorical variables into numerical variables
df["species_codes"] = pd.Categorical(df["species"]).codes
How to transform categorical text variables into integers

More on DataFrames

If you want to know more about DataFrame and Pandas. Checkout the other articles I wrote on the topic, just here :

Pandas - The Python You Need
We gathered the only Python essentials that you will probably ever need.