0
0
Pandasdata~20 mins

Ordered categories in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 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.