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”.
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.
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.
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.