An ordered category is a type of categorical data where the categories have a meaningful order or ranking, like 'low' < 'medium' < 'high'.
Click to reveal answer
beginner
How do you create an ordered categorical column in pandas?
Use pd.Categorical() with the ordered=True parameter and specify the category order with the categories argument.
Click to reveal answer
beginner
Why use ordered categories instead of strings or numbers?
Ordered categories save memory and allow meaningful comparisons like <, >, and sorting based on the order you define.
Click to reveal answer
intermediate
What happens if you compare two ordered categorical values in pandas?
Pandas compares them based on the order you set, not alphabetically or numerically. For example, 'low' < 'high' if 'low' is before 'high' in the category order.
Click to reveal answer
intermediate
How can you change the order of categories in an existing ordered categorical column?
Use the cat.reorder_categories() method with the new order list and ordered=True to update the order.
Click to reveal answer
Which parameter makes a pandas categorical ordered?
Aorder='asc'
Bordered=True
Csort=True
Drank=True
✗ Incorrect
The parameter ordered=True tells pandas the categories have a meaningful order.
What is the output type when you use pd.Categorical(['low', 'high'], categories=['low', 'medium', 'high'], ordered=True)?
AA pandas Series
BA numpy array
CA list
DAn ordered categorical object
✗ Incorrect
This creates an ordered categorical object with the specified categories and order.
If 'medium' is between 'low' and 'high' in order, what is the result of 'medium' > 'low'?
ATrue
BFalse
CError
DDepends on data type
✗ Incorrect
Because 'medium' comes after 'low' in the order, the comparison returns True.
How do ordered categories help in sorting data?
AThey convert categories to numbers before sorting.
BThey sort data randomly.
CThey allow sorting based on the defined order, not alphabetically.
DThey do not affect sorting.
✗ Incorrect
Ordered categories let pandas sort data according to the category order you set.
Which method changes the order of categories in a pandas categorical column?
Acat.reorder_categories()
Bcat.set_order()
Ccat.sort_categories()
Dcat.change_order()
✗ Incorrect
The cat.reorder_categories() method updates the order of categories.
Explain what ordered categories are and why they are useful in pandas.
Think about how you rank things in real life, like sizes or ratings.
You got /4 concepts.
Describe how to create and modify an ordered categorical column in pandas.
Focus on the parameters and methods pandas provides.
You got /4 concepts.
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?