📖 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
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
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
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
Hint
Use sizes.sort_values() to sort and then print() to show the sorted Series.
Practice
(1/5)
1. What does setting ordered=True do when creating a pandas categorical variable?
easy
A. It converts categories to numeric values automatically.
B. It sorts the categories alphabetically.
C. It defines a natural order for the categories, allowing comparisons.
D. It removes duplicate categories from the list.
Solution
Step 1: Understand the role of ordered categories
Ordered categories allow pandas to know the sequence of categories, so it can compare them meaningfully.
Step 2: Effect of ordered=True
Setting ordered=True tells pandas to treat the categories as having a specific order, enabling operations like greater than or less than.
Final Answer:
It defines a natural order for the categories, allowing comparisons. -> Option C
Quick Check:
ordered=True means categories have order [OK]
Hint: Ordered=True means categories can be compared by order [OK]
Common Mistakes:
Thinking it sorts categories automatically
Assuming it converts categories to numbers
Believing it removes duplicates
2. Which of the following is the correct way to create an ordered categorical variable in pandas with categories 'low', 'medium', 'high' in that order?
easy
A. pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium', 'high'], ordered=True)
B. pd.Categorical(['low', 'tiny', 'high'], categories=['low', 'medium', 'high'], ordered=True)
C. pd.Categorical(['low', 'medium', 'high'], ordered=True, categories=['low', 'medium', 'high'])
D. pd.Categorical(['low', 'medium', 'high'], ordered=False, categories=['low', 'medium', 'high'])
Solution
Step 1: Check parameter order and names
pd.Categorical takes data first, then categories, then ordered flag as keyword arguments.
Step 2: Identify correct syntax
pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium', 'high'], ordered=True) correctly passes data, categories list, and ordered=True in correct order and names.
Final Answer:
pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium', 'high'], ordered=True) -> Option A
Quick Check:
Correct syntax uses categories= and ordered=True [OK]
Hint: Use categories= first, then ordered=True keyword [OK]
A. Comparison operator '>' is not supported for categorical data.
B. The categories list is missing 'high', causing invalid comparison.
C. Ordered=True cannot be used with categories parameter.
D. The data list has duplicate values.
Solution
Step 1: Check categories vs data
Data contains 'high' but categories only include 'low' and 'medium'.
Step 2: Understand effect on comparison
'high' is not in categories, so comparison with 'medium' fails causing error.
Final Answer:
The categories list is missing 'high', causing invalid comparison. -> Option B
Quick Check:
Data must match categories exactly [OK]
Hint: Ensure all data values are in categories list [OK]
Common Mistakes:
Thinking ordered=True disallows categories
Believing '>' is unsupported for ordered categories
Ignoring missing categories in list
5. You have a DataFrame with a column 'size' containing values 'small', 'medium', 'large', and 'extra large'. You want to sort the DataFrame by 'size' in the natural order small < medium < large < extra large. Which code correctly achieves this?