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

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
Examples
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 print(df)
Output:
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”.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Let’s apply both methods to the “Size1” column.
# unique values in "Size1" print(df["Size1"].unique()) print("----") # categories in "Size1" print(df["Size1"].cat.categories)
Output:
['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.
Now let’s apply these methods to the “Size2” column.
# unique values in "Size2" print(df["Size2"].unique()) print("----") # categories in "Size2" print(df["Size2"].cat.categories)
Output:
['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 –
- Pandas Category Column to a List
- Pandas – Set Category Order of a Categorical Column
- Get List of Categories in Pandas Category Column
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.