Recall & Review
beginner
What is a category in pandas and why use it?
A category in pandas is a special data type for variables with a fixed set of possible values. It saves memory and speeds up operations when you have repeated values like colors or types.
Click to reveal answer
beginner
How do you add a new category to a pandas Categorical column?
Use the method
cat.add_categories() on the column. For example, df['col'] = df['col'].cat.add_categories(['new_cat']) adds 'new_cat' as a new category.Click to reveal answer
beginner
How can you remove a category from a pandas Categorical column?
Use
cat.remove_categories() method. For example, df['col'] = df['col'].cat.remove_categories(['old_cat']) removes 'old_cat' from categories.Click to reveal answer
intermediate
What happens if you remove a category that is currently used in the data?
The values in that category become
NaN (missing) because the category no longer exists in the list of allowed categories.Click to reveal answer
intermediate
How do you reset categories to only those currently used in the data?
Use
cat.remove_unused_categories() to drop categories not used in the data, cleaning up the category list.Click to reveal answer
Which pandas method adds a new category to a Categorical column?
✗ Incorrect
The correct method to add categories is
cat.add_categories().What happens to data values in a category that is removed using
cat.remove_categories()?✗ Incorrect
Removing a category causes its values to become
NaN because the category no longer exists.How do you remove categories that are not used in the data?
✗ Incorrect
The method
cat.remove_unused_categories() removes categories not present in the data.Why use categories in pandas?
✗ Incorrect
Categories save memory and speed up operations for repeated values.
Which of these is NOT a valid way to modify categories in pandas?
✗ Incorrect
There is no
cat.update_categories() method in pandas.Explain how to add and remove categories in a pandas Categorical column. Include what happens to data when categories are removed.
Think about how categories control allowed values and what happens if you remove one.
You got /3 concepts.
Describe why and how you would clean up unused categories in a pandas Categorical column.
Consider categories that no longer appear in the data.
You got /3 concepts.