0
0
Pandasdata~5 mins

Ordered categories in Pandas

Choose your learning style9 modes available
Introduction

Ordered categories help us sort or compare data that has a natural order, like sizes or ratings.

When you have data like 'small', 'medium', 'large' and want to sort it properly.
When you want to compare survey answers like 'poor', 'average', 'good', 'excellent'.
When you need to plot data with categories in a specific order.
When you want to group data by levels that have a ranking.
Syntax
Pandas
pd.Categorical(data, categories=[list_of_categories], ordered=True)

Set ordered=True to tell pandas the categories have a specific order.

The categories list defines the order from lowest to highest.

Examples
This creates an ordered category for sizes with the order small < medium < large.
Pandas
import pandas as pd
sizes = ['small', 'large', 'medium', 'small']
cat_sizes = pd.Categorical(sizes, categories=['small', 'medium', 'large'], ordered=True)
print(cat_sizes)
Here, ratings are ordered from poor to excellent.
Pandas
import pandas as pd
ratings = ['good', 'poor', 'excellent', 'average']
cat_ratings = pd.Categorical(ratings, categories=['poor', 'average', 'good', 'excellent'], ordered=True)
print(cat_ratings)
Sample Program

This program creates an ordered category for shirt sizes, then sorts the DataFrame so sizes appear from small to large.

Pandas
import pandas as pd

# Create a list of shirt sizes
sizes = ['medium', 'small', 'large', 'medium', 'small']

# Define ordered categories
ordered_sizes = pd.Categorical(sizes, categories=['small', 'medium', 'large'], ordered=True)

# Create a DataFrame
df = pd.DataFrame({'Size': ordered_sizes})

# Sort the DataFrame by Size
sorted_df = df.sort_values('Size')

print(sorted_df)
OutputSuccess
Important Notes

If you don't set ordered=True, pandas treats categories as unordered and sorting won't work as expected.

You can compare ordered categories using operators like <, >, ==.

Summary

Ordered categories let you work with data that has a natural order.

Use pd.Categorical with ordered=True and specify the order.

This helps in sorting, comparing, and plotting categorical data correctly.