difference between pandas series.unique() and pandas series.cat.categories

Pandas Series.unique() vs Series.cat.categories

In this tutorial, we will look at the difference between the Pandas Series.unique() function and the Pandas Series.cat.categories attribute for a category type column.

Difference between Pandas Series.unique() and Series.cat.categories

difference between pandas series.unique() and pandas series.cat.categories

The key difference is that, the pandas Series.unique() function gives us the unique value occurring in the data whereas the pandas.cat.categories property returns the possible categories for a category type column


Let’s look at some examples to illustrate the difference between the two methods. 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({
        "Size1": ["S", "S", "L", "M", "L"],
        "Size2": ["S", "M", "M", "S", "M"]

# change to category dtype
df["Size1"] = df["Size1"].astype("category")
df["Size2"] = df["Size2"].astype("category")
# set catgories for "Size2"
df["Size2"] = df["Size2"].cat.set_categories(["S", "M", "L"])

# display the dataframe


  Size1 Size2
0     S     S
1     S     M
2     L     M
3     M     S
4     L     M

In the above dataframe, we have two category type columns -“Size1” and “Size1”. Note that the possible category values for both “Size1” and “Size2” are the same – “S”, “M”, and “L”.

Let’s apply both methods to the “Size1” column.

# unique values in "Size1"
# categories in "Size1"


['S', 'L', 'M']
Categories (3, object): ['L', 'M', 'S']
Index(['L', 'M', 'S'], dtype='object')

You can see that we get the same values from both methods. The Series.unique() method gives the unique value in the data which is the same as the possible category values.

📚 Data Science Programs By Skill Level


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.

Now let’s apply these methods to the “Size2” column.

# unique values in "Size2"
# categories in "Size2"


['S', 'M']
Categories (3, object): ['S', 'M', 'L']
Index(['S', 'M', 'L'], dtype='object')

We get different results. This is because, the data in the column “Size2” contains only “S” and “M” values, which is the result that we get from the Pandas Series.unique() function. On the other hand, the Pandas Series.cat.categories property gives all the possible category values, which are – “S”, “M”, “L”.

These methods may give the same results at times but they are intended for different purposes.

  • Use the Pandas Series.unique() function to get the unique values in the data (in the Pandas series).
  • And use the Pandas Series.cat.categories property to get the categories for a category type column in Pandas.

In this tutorial, we looked at the difference between the two methods – Pandas Series.unique() and Pandas Series.cat.categories.

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.


  • 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