Bird
Raised Fist0
Pandasdata~20 mins

Ordered categories in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ordered Categories Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of ordered categorical sorting

What is the output of this code snippet?

import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high', 'medium', 'low'], categories=['low', 'medium', 'high'], ordered=True)
s = pd.Series(cats)
s_sorted = s.sort_values()
print(s_sorted.tolist())
Pandas
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high', 'medium', 'low'], categories=['low', 'medium', 'high'], ordered=True)
s = pd.Series(cats)
s_sorted = s.sort_values()
print(s_sorted.tolist())
A['high', 'medium', 'medium', 'low', 'low']
B['low', 'low', 'medium', 'medium', 'high']
C['low', 'medium', 'high', 'medium', 'low']
D['medium', 'medium', 'low', 'low', 'high']
Attempts:
2 left
💡 Hint

Remember that ordered categories sort according to the order defined in categories.

❓ data_output
intermediate
1:30remaining
Number of elements less than 'medium' in ordered category

Given this ordered categorical series, how many elements are less than 'medium'?

import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high', 'medium', 'low'], categories=['low', 'medium', 'high'], ordered=True)
s = pd.Series(cats)
count = (s < 'medium').sum()
print(count)
Pandas
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high', 'medium', 'low'], categories=['low', 'medium', 'high'], ordered=True)
s = pd.Series(cats)
count = (s < 'medium').sum()
print(count)
A0
B3
C1
D2
Attempts:
2 left
💡 Hint

Check which values are strictly less than 'medium' in the order.

🔧 Debug
advanced
1:30remaining
Identify the error in creating ordered categories

What error does this code raise?

import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high', 'medium', 'low'], categories=['low', 'medium'], ordered=True)
Pandas
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high', 'medium', 'low'], categories=['low', 'medium'], ordered=True)
AValueError: 'high' is not in categories
BValueError: items in categories are not in the data
CNo error, code runs fine
DTypeError: categories must be a list
Attempts:
2 left
💡 Hint

Check if all values in the data are included in the categories list.

🚀 Application
advanced
2:00remaining
Using ordered categories to compare values

Which option correctly creates an ordered categorical series and checks if each value is greater than or equal to 'medium'?

A
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high'], categories=['high', 'medium', 'low'], ordered=True)
s = pd.Series(cats)
result = s &gt;= 'medium'
print(result.tolist())
B
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high'], ordered=True)
s = pd.Series(cats)
result = s &gt; 'medium'
print(result.tolist())
C
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium', 'high'], ordered=True)
s = pd.Series(cats)
result = s &gt;= 'medium'
print(result.tolist())
D
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium', 'high'])
s = pd.Series(cats)
result = s &gt;= 'medium'
print(result.tolist())
Attempts:
2 left
💡 Hint

Ordered categories require the categories list to be in the correct order and ordered=True.

🧠 Conceptual
expert
1:30remaining
Why use ordered categories in data analysis?

Which is the best reason to use ordered categories in a dataset?

ATo enable meaningful comparison and sorting of categorical data with a logical order
BTo convert categorical data into numerical data for machine learning models
CTo allow missing values to be automatically filled with the most frequent category
DTo save memory by storing data as integers internally
Attempts:
2 left
💡 Hint

Think about what ordered categories add beyond normal categories.

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

  1. Step 1: Understand the role of ordered categories

    Ordered categories allow pandas to know the sequence of categories, so it can compare them meaningfully.
  2. 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.
  3. Final Answer:

    It defines a natural order for the categories, allowing comparisons. -> Option C
  4. 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

  1. Step 1: Check parameter order and names

    pd.Categorical takes data first, then categories, then ordered flag as keyword arguments.
  2. 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.
  3. Final Answer:

    pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium', 'high'], ordered=True) -> Option A
  4. Quick Check:

    Correct syntax uses categories= and ordered=True [OK]
Hint: Use categories= first, then ordered=True keyword [OK]
Common Mistakes:
  • Passing ordered=True before categories
  • Using ordered=False for ordered categories
  • Not matching categories list with data
3. What will be the output of this code?
import pandas as pd
cats = pd.Categorical(['medium', 'low', 'high'], categories=['low', 'medium', 'high'], ordered=True)
print(cats > 'low')
medium
A. [False, True, True]
B. [False, False, True]
C. [True, True, False]
D. [True, False, True]

Solution

  1. Step 1: Understand the categories and order

    Categories are ordered as low < medium < high.
  2. Step 2: Compare each element to 'low'

    'medium' > 'low' is True, 'low' > 'low' is False, 'high' > 'low' is True.
  3. Final Answer:

    [True, False, True] -> Option D
  4. Quick Check:

    medium and high are greater than low [OK]
Hint: Compare each category to 'low' using order [OK]
Common Mistakes:
  • Confusing category order
  • Assuming string comparison instead of categorical
  • Mixing True/False positions
4. The following code raises an error. What is the cause?
import pandas as pd
cats = pd.Categorical(['low', 'medium', 'high'], categories=['low', 'medium'], ordered=True)
print(cats > 'medium')
medium
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

  1. Step 1: Check categories vs data

    Data contains 'high' but categories only include 'low' and 'medium'.
  2. Step 2: Understand effect on comparison

    'high' is not in categories, so comparison with 'medium' fails causing error.
  3. Final Answer:

    The categories list is missing 'high', causing invalid comparison. -> Option B
  4. 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?
hard
A. df['size'] = pd.Categorical(df['size'], categories=['small', 'medium', 'large', 'extra large'], ordered=True) df.sort_values('size')
B. df['size'] = pd.Categorical(df['size'], ordered=True) df.sort_values('size')
C. df['size'] = pd.Categorical(df['size'], categories=['extra large', 'large', 'medium', 'small'], ordered=True) df.sort_values('size')
D. df.sort_values('size') # No need to set categories

Solution

  1. Step 1: Define ordered categories with correct order

    Set categories=['small', 'medium', 'large', 'extra large'] with ordered=True to define natural order.
  2. Step 2: Sort DataFrame by the ordered categorical column

    Use df.sort_values('size') to sort according to the defined order.
  3. Final Answer:

    df['size'] = pd.Categorical(df['size'], categories=['small', 'medium', 'large', 'extra large'], ordered=True) df.sort_values('size') -> Option A
  4. Quick Check:

    Set ordered categories then sort [OK]
Hint: Set ordered categories with correct order before sorting [OK]
Common Mistakes:
  • Not specifying categories order
  • Using wrong category order
  • Trying to sort without ordered categories
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard