In this tutorial, we will look at how to check if a column in a Pandas dataframe is a category type column or not with the help of some examples.
How to check if a Pandas column is a category column or not?
You can compare the column’s dtype with “category” to check whether a column is a category type column or not in Pandas. The following is the syntax –
# check if column is a category column df["Col"].dtype == "category" # alternatively, you can also use hasattr() hasattr(df["Col"], "cat")
It returns True
if the column is of category
dtype.
Examples
Let’s look at some examples of using the above methods. First, we will create a sample dataframe that we will be using throughout this tutorial.
import pandas as pd # create a dataframe df = pd.DataFrame({ "Name": ["Tim", "Sarah", "Hasan", "Jyoti", "Jack"], "Shirt Size": ["Small", "Medium", "Large", "Small", "Large"] }) # change to category dtype df["Shirt Size"] = df["Shirt Size"].astype("category") # display the dataframe print(df)
Output:
Name Shirt Size 0 Tim Small 1 Sarah Medium 2 Hasan Large 3 Jyoti Small 4 Jack Large
We now have a dataframe containing the name and t-shirt size information of some students in a university. Note that in the above code we converted the “Shirt Size” column to category
dtype using the Pandas astype() function.
Check if a column is a Pandas categorical column
Let’s now check whether the “Shirt Size” column is a category type column or not. We use the above syntax to compare the column’s dtype with “category”.
# check if pandas column is of category type print(df["Shirt Size"].dtype == "category")
Output:
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.
True
We get True
as the output indicating that the column is of category
dtype.
Check if a Pandas series contains categorical data using hasattr()
Alternatively, you can also use the Python hasattr()
function to check if a Pandas series contains categorical data or not (which is the same as checking whether it’s a category type column or not).
# check if pandas column is of category type print(hasattr(df["Shirt Size"], "cat"))
Output:
True
Here, we check whether the “Shirt Size” column in the above dataframe has a “cat” attribute. We get True
as the output, indicating that the column has a “cat” attribute and thus is a category type column in Pandas.
You might also be interested in –
- Get List of Categories in Pandas Category Column
- Pandas – Set Category Order of a Categorical Column
- Pandas – Rename Categories in Category Column
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.