0
0
Pandasdata~30 mins

Ordered categories in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Ordered categories
📖 Scenario: You work in a store that sells clothes. You have a list of T-shirt sizes from customers. The sizes are small, medium, and large. You want to organize these sizes in order so you can analyze them better.
🎯 Goal: Create a pandas Series with T-shirt sizes as an ordered category. Then, sort the sizes in the correct order: small, medium, large.
📋 What You'll Learn
Create a pandas Series called sizes with the values: 'medium', 'small', 'large', 'medium', 'small'.
Create an ordered categorical type called size_order with categories: 'small', 'medium', 'large' in this order.
Convert the sizes Series to use the ordered categorical type size_order.
Sort the sizes Series by the ordered categories.
Print the sorted sizes Series.
💡 Why This Matters
🌍 Real World
Stores often have product sizes or ratings that have a natural order. Using ordered categories helps sort and analyze such data correctly.
💼 Career
Data analysts and scientists use ordered categories to prepare and clean data for reports and machine learning models.
Progress0 / 4 steps
1
Create the sizes Series
Import pandas as pd. Create a pandas Series called sizes with these exact values in this order: 'medium', 'small', 'large', 'medium', 'small'.
Pandas
Need a hint?

Use pd.Series and pass the list of sizes exactly as given.

2
Create the ordered category type
Create an ordered categorical type called size_order using pd.CategoricalDtype with categories 'small', 'medium', 'large' in this order. Make sure it is ordered.
Pandas
Need a hint?

Use pd.CategoricalDtype with the categories list and ordered=True.

3
Convert sizes to ordered category
Convert the sizes Series to use the ordered categorical type size_order by using astype(size_order) and assign it back to sizes.
Pandas
Need a hint?

Use sizes.astype(size_order) to convert the Series.

4
Sort and print the sizes
Sort the sizes Series using sort_values() and print the sorted Series.
Pandas
Need a hint?

Use sizes.sort_values() to sort and then print() to show the sorted Series.