pandas get min value in ordered categorical series

Pandas – Get Min Value in Ordered Categorical Column

In this tutorial, we will look at how to get the min value in an ordered categorical column (or series) in Pandas.

How to get the min value in an ordered categorical column?

pandas get min value in ordered categorical series

You can apply the Pandas series min() function to get the min value in a categorical Pandas column (or a series). The following is the syntax –

# s is a categorical type ordered pandas series
s.min()

It returns the minimum value in the series based on the categorical order. If the categorical data is not ordered, it will result in a TypeError.

Examples

Let’s look at some examples of using the above method to get the minimum value in a category type series in Pandas.

Applying the min() function to an unordered categorical field in Pandas

First, let’s see what happens if we apply the min() function to an unordered categorical type series in Pandas.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
        "Name": ["Tim", "Sarah", "Hasan", "Jyoti", "Jack"],
        "Shirt Size": ["M", "S", "M", "M", "L"]
})
# change to category dtype
df["Shirt Size"] = df["Shirt Size"].astype("category")
# get the min value in shirt size
print(df["Shirt Size"].min())

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [7], in <module>
      9 df["Shirt Size"] = df["Shirt Size"].astype("category")
     10 # get the min value in shirt size
---> 11 print(df["Shirt Size"].min())

TypeError: Categorical is not ordered for operation min
you can use .as_ordered() to change the Categorical to an ordered one

We get a TypeError. Here, we first create a Pandas dataframe with names and shirt sizes of students in a university. We then convert the “Shirt Size” column to category dtype. And finally, we apply the min() function to the “Shirt Size” column.

All categorical fields, by default, are unordered unless specified otherwise. We get a TypeError because there’s no way to compare one categorical value with another for an unordered series and thus computing the min value doesn’t make any sense.

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

Min value in an ordered category field in Pandas

Let’s now modify the “Shirt Size” column to an ordered categorical field with the order of sizes as “S” < “M” < “L”.

# set and order categories for the shirt size column
df["Shirt Size"] = df["Shirt Size"].cat.set_categories(["S", "M", "L"], ordered=True)
# display the shirt size column
print(df["Shirt Size"])

Output:

0    M
1    S
2    M
3    M
4    L
Name: Shirt Size, dtype: category
Categories (3, object): ['S' < 'M' < 'L']

The “Shirt Size” column is now ordered. Let’s now get the minimum value in the column with the min() function.

# get the min value in shirt size
print(df["Shirt Size"].min())

Output:

S

We get “S” as the minimum value.

Let’s look at another example. What if the possible values in a categorical series are “S”, “M”, and “L” but the data contains only “M” and “L”, what do you think we’d get on applying the min() function?

# create a pandas series
shirt_size = pd.Series(["M", "L", "L", "M"], dtype="category")
# set and order categories
shirt_size = shirt_size.cat.set_categories(["S", "M", "L"], ordered=True)
# get the min value in the series
print(shirt_size.min())

Output:

M

We get “M” as the minimum value because it is the minimum value that occurs in our data.

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