0
0
Pandasdata~5 mins

Adding and removing categories in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Acat.append_categories()
Bcat.insert_categories()
Ccat.add_categories()
Dcat.new_category()
What happens to data values in a category that is removed using cat.remove_categories()?
AThey are duplicated
BThey become NaN (missing)
CThey are converted to strings
DThey stay unchanged
How do you remove categories that are not used in the data?
Acat.remove_unused_categories()
Bcat.drop_unused()
Ccat.clean_categories()
Dcat.trim_categories()
Why use categories in pandas?
ATo sort numbers faster
BTo store images
CTo create plots automatically
DTo save memory and speed up operations
Which of these is NOT a valid way to modify categories in pandas?
Acat.update_categories()
Bcat.remove_categories()
Ccat.add_categories()
Dcat.remove_unused_categories()
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.