0
0
Pandasdata~20 mins

Category codes and labels in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Category Codes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1 0 1 2]
B[0 1 0 2]
C[1 2 1 0]
D[2 1 2 0]
Attempts:
2 left
💡 Hint
Remember that codes correspond to the position of each category in the categories list.
data_output
intermediate
2: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))
A3
B1
C2
D4
Attempts:
2 left
💡 Hint
Unused categories are those not present in the data values.
🔧 Debug
advanced
2: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)
AIndexError: list index out of range
BNo error, output: [0 1 -1]
CValueError: 'bird' is not in categories
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint
Check how pandas handles values not in categories.
🚀 Application
advanced
2: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))
A['small', 'medium', 'large', 'small']
B['medium', 'large', 'small', 'medium']
C['large', 'medium', 'small', 'large']
D['large', 'small', 'medium', 'large']
Attempts:
2 left
💡 Hint
Codes map to categories by index position.
🧠 Conceptual
expert
2: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?
ACodes become invalid and raise an error
BCodes remain the same, but category labels change
CCodes are recalculated to match the new category order
DCodes are reset to zero for all entries
Attempts:
2 left
💡 Hint
Think about how codes represent category positions.