0
0
Pandasdata~20 mins

Adding and removing categories in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Categorical Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adding a new category to a pandas Categorical
What is the output of this code snippet?
Pandas
import pandas as pd
cat = pd.Categorical(['apple', 'banana'], categories=['apple', 'banana'])
cat = cat.add_categories(['cherry'])
print(cat.categories.tolist())
A['apple', 'banana', 'cherry']
B['apple', 'banana']
C['cherry']
D['apple', 'banana', 'cherry', 'date']
Attempts:
2 left
💡 Hint
Think about what add_categories does to the categories list.
data_output
intermediate
2:00remaining
Result of removing a category from a pandas Categorical
What is the output of this code?
Pandas
import pandas as pd
cat = pd.Categorical(['apple', 'banana', 'cherry'], categories=['apple', 'banana', 'cherry'])
cat = cat.remove_categories(['banana'])
print(cat.categories.tolist())
A['apple', 'cherry']
B['apple', 'banana']
C['banana']
D['apple', 'banana', 'cherry']
Attempts:
2 left
💡 Hint
Removing a category deletes it from the categories list.
🔧 Debug
advanced
2:00remaining
Behavior when removing a non-existent category
What happens when running this code?
Pandas
import pandas as pd
cat = pd.Categorical(['apple', 'banana'], categories=['apple', 'banana'])
cat = cat.remove_categories(['cherry'])
AValueError because 'cherry' is not in the categories
BTypeError because remove_categories expects a string, not a list
CNo error, code runs fine
DKeyError because 'cherry' is missing
Attempts:
2 left
💡 Hint
remove_categories ignores categories that do not exist.
🚀 Application
advanced
2:00remaining
Effect of removing a category on data values
Given this code, what is the output of the print statement?
Pandas
import pandas as pd
cat = pd.Categorical(['apple', 'banana', 'cherry', 'banana'], categories=['apple', 'banana', 'cherry'])
cat = cat.remove_categories(['banana'])
print(cat.tolist())
A['apple', 'cherry', 'cherry', 'cherry']
B['apple', nan, 'cherry', nan]
C['apple', 'banana', 'cherry', 'banana']
D['apple', None, 'cherry', None]
Attempts:
2 left
💡 Hint
Removing a category replaces its values with NaN in the data.
🧠 Conceptual
expert
2:00remaining
Why use add_categories instead of directly assigning new categories?
Why is it better to use add_categories to add new categories instead of assigning a new categories list directly?
ADirect assignment automatically updates data values to new categories
BDirect assignment is faster and safer than add_categories
Cadd_categories removes old categories automatically
Dadd_categories preserves existing data values and only adds new categories, avoiding errors
Attempts:
2 left
💡 Hint
Think about what happens to existing data when categories change.