check if column is of category type in pandas using .dtype

Check If Pandas Column is of Category Type

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top