Challenge - 5 Problems
Category Codes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of category codes after reordering categories
What is the output of the following code snippet?
Pandas
import pandas as pd cats = pd.Categorical(['apple', 'banana', 'apple', 'orange'], categories=['banana', 'apple', 'orange'], ordered=True) print(cats.codes)
Attempts:
2 left
💡 Hint
Remember that codes correspond to the position of each category in the categories list.
✗ Incorrect
The categories are ordered as ['banana', 'apple', 'orange']. 'apple' is at index 1, 'banana' at 0, and 'orange' at 2. So the codes for ['apple', 'banana', 'apple', 'orange'] are [1, 0, 1, 2].
❓ data_output
intermediate2:00remaining
Number of unique categories after removing unused categories
Given the following categorical series, how many categories remain after removing unused categories?
Pandas
import pandas as pd s = pd.Series(pd.Categorical(['red', 'blue', 'red', 'green'], categories=['red', 'blue', 'green', 'yellow'])) s = s.cat.remove_unused_categories() print(len(s.cat.categories))
Attempts:
2 left
💡 Hint
Unused categories are those not present in the data values.
✗ Incorrect
The original categories are ['red', 'blue', 'green', 'yellow']. The data contains 'red', 'blue', and 'green' but not 'yellow'. After removing unused categories, 'yellow' is dropped, leaving 3 categories.
🔧 Debug
advanced2:00remaining
Identify the error in category assignment
What error does the following code raise?
Pandas
import pandas as pd cats = pd.Categorical(['cat', 'dog', 'bird'], categories=['cat', 'dog']) print(cats.codes)
Attempts:
2 left
💡 Hint
Check how pandas handles values not in categories.
✗ Incorrect
Pandas assigns code -1 to values not in the categories list. So 'bird' gets code -1, no error is raised.
🚀 Application
advanced2:00remaining
Mapping category codes to labels
You have a categorical series with codes [2, 0, 1, 2] and categories ['small', 'medium', 'large']. Which code produces the correct list of labels?
Pandas
import pandas as pd codes = [2, 0, 1, 2] categories = ['small', 'medium', 'large'] cats = pd.Categorical.from_codes(codes, categories) print(list(cats))
Attempts:
2 left
💡 Hint
Codes map to categories by index position.
✗ Incorrect
Code 2 maps to 'large', 0 to 'small', and 1 to 'medium'. So the list is ['large', 'small', 'medium', 'large'].
🧠 Conceptual
expert2:00remaining
Effect of changing categories order on codes
If you change the order of categories in a pandas Categorical object, what happens to the codes attribute?
Attempts:
2 left
💡 Hint
Think about how codes represent category positions.
✗ Incorrect
Codes represent the index of each value in the categories list. Changing the order changes indices, so codes are recalculated accordingly.