In this tutorial, we will look at how to check if a pandas series with category dtype is ordered or not with the help of some examples.
Ordered and unordered categorical values
A categorical field may or may not be ordered. For example, gender values “M” and “F” do not have an order to them and can be considered as an unordered categorical field.
Some categorical fields on the other hand are ordered. For example, t-shirt sizes, S, M, L, and XL. These values are categorical but they also have an order to them, S < M < L < XL.
The categorical data type in pandas is used to store categorical data. It also allows you to specify an order to the values (if any).
How to check if a pandas categorical data is ordered or not?
You can use the
ordered property of a Pandas categorical data to check if the categories in the data are ordered or not. The following is the syntax –
# s is pandas series with category dtype s.cat.ordered
It returns a boolean value representing whether the given categorical data is ordered or not.
Let’s look at some examples of checking for order in Pandas categorical data.
import pandas as pd # create a pandas series with category dtype shirt_size = pd.Series(["M", "S", "L", "M", "XL"], dtype="category") # check if the category is ordered print(shirt_size.cat.ordered)
Here, we create a Pandas series with
category dtype. We then check if it’s ordered or not by accessing its
ordered property. You can see that we get
False as the output. The category is unordered by default since we didn’t specify it to be ordered during creation.
Let’s now look at another example. Here let’s create a pandas category that is ordered using the
CategoricalDtype and then check whether it’s ordered or not.
from pandas.api.types import CategoricalDtype # create an ordered category type for shirt size cat_type = CategoricalDtype(categories=["S", "M", "L", "XL"], ordered=True) # create a pandas series of shirt sizes shirt_size = pd.Series(["M", "S", "L", "M", "XL"]) # change the series type to the custom category type shirt_size = shirt_size.astype(cat_type) # check if category is ordered print(shirt_size.cat.ordered)
You can see that we get
True as the output because the category is ordered. If you display the series, you can see the category values and their order.
# display the series print(shirt_size)
0 M 1 S 2 L 3 M 4 XL dtype: category Categories (4, object): ['S' < 'M' < 'L' < 'XL']
We get the order ‘S’ < ‘M’ < ‘L’ < ‘XL’ of the categories in the series.
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.