In this tutorial, we will look at how to convert a category type column in Pandas to an integer type column with the help of some examples.
How to convert category to int type in Pandas?
You can use the Pandas astype()
function to change the data type of a column. To convert a category type column to integer type, apply the astype()
function on the column and pass 'int'
as the argument. The following is the syntax –
# convert pandas column to int type df["Col"] = df["Col"].astype("int")
It changes the type of the column to int. Note that if the individual values in the column cannot be converted to integers, it will result in an error. For example, 1
and "1"
can be converted to integer but "one"
cannot be converted.
Examples
Let’s look at some examples of converting category type column(s) to integer type in Pandas. First, we will create a Pandas dataframe that we’ll be using throughout this tutorial.
import pandas as pd # create a dataframe df = pd.DataFrame({ "Name": ["Tim", "Sarah", "Hasan", "Jyoti", "Jack"], "Class": [1, 2, 2, 3, 1] }) # change to category dtype df["Class"] = df["Class"].astype("category") # display the dataframe print(df)
Output:
Name Class 0 Tim 1 1 Sarah 2 2 Hasan 2 3 Jyoti 3 4 Jack 1
We have a dataframe containing the names and the class (or grades) of some students in a primary school.
# display the "Class" column print(df["Class"])
Output:
0 1 1 2 2 2 3 3 4 1 Name: Class, dtype: category Categories (3, int64): [1, 2, 3]
The “Class” column in the above dataframe is of category
type with individual values as integers.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s convert this column from category to int type using the Pandas astype()
function.
# category column to integer df["Class"] = df["Class"].astype("int") # display the "Class" column print(df["Class"])
Output:
0 1 1 2 2 2 3 3 4 1 Name: Class, dtype: int64
You can see that the column is now of int64
type.
Category column with non-numerical values to integer
In the above example, the individual values in the category column were numeric. What if you try to convert a category column with non-numeric values to an int
type column? Let’s find out.
# add a new column to store the class in words df["Class2"] = ["First", "Second", "Second", "Third", "First"] # convert column to category type df["Class2"] = df["Class2"].astype("category") # display the column print(df["Class2"])
Output:
0 First 1 Second 2 Second 3 Third 4 First Name: Class2, dtype: category Categories (3, object): ['First', 'Second', 'Third']
We added an additional column to our dataframe. The “Class2” column stores the class values in words. For example, class 1 is stored as “First”, class 2 is stored as “Second” and so on.
Let’s try to convert the “Class2” column to integer type. We’ll use the same syntax as above.
# category column to integer df["Class2"] = df["Class2"].astype("int") # display the "Class2" column print(df["Class2"])
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [8], in <module> 1 # category column to integer ----> 2 df["Class2"] = df["Class2"].astype("int") 3 # display the "Class2" column 4 print(df["Class2"]) ... ValueError: Cannot cast object dtype to int64
We get a ValueError
because the values in the column cannot be converted to integers.
You might also be interested in –
- Pandas – Rename Categories in Category Column
- Pandas – Convert Category Type Column to String
- Count Frequency of Category Values in Pandas
- Pandas – Check If Category is Ordered
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.