📖 Scenario: You work in a small company that tracks product categories for sales analysis. Sometimes new categories are added, and sometimes old categories are removed. You want to learn how to manage these categories easily using pandas.
🎯 Goal: Build a pandas Series with product categories, then add a new category and remove an old category from the categories list.
📋 What You'll Learn
Create a pandas Series with categorical data
Add a new category to the Series categories
Remove an existing category from the Series categories
Print the final categories list
💡 Why This Matters
🌍 Real World
Managing product categories is common in sales and inventory systems. Categories can change over time, so knowing how to add or remove them helps keep data accurate.
💼 Career
Data analysts and data scientists often work with categorical data. Understanding how to manipulate categories in pandas is essential for cleaning and preparing data for analysis.
Progress0 / 4 steps
1
Create a pandas Series with product categories
Import pandas as pd and create a pandas Series called products with these categories: 'Electronics', 'Clothing', 'Toys'. Make sure the Series has categorical dtype.
Pandas
Hint
Use pd.Series() with dtype='category' to create a categorical Series.
2
Add a new category called 'Books'
Add a new category 'Books' to the categories of the products Series using the add_categories() method.
Pandas
Hint
Use products.cat.add_categories(['Books']) to add the new category.
3
Remove the category 'Toys'
Remove the category 'Toys' from the categories of the products Series using the remove_categories() method.
Pandas
Hint
Use products.cat.remove_categories(['Toys']) to remove the category.
4
Print the final categories
Print the categories of the products Series using products.cat.categories.
Pandas
Hint
Use print(products.cat.categories) to see the current categories.
Practice
(1/5)
1. What does the add_categories() method do in pandas categorical data?
easy
A. It converts a column to a categorical type.
B. It removes categories from a categorical column.
C. It adds new possible categories to an existing categorical column.
D. It renames existing categories in a categorical column.
Solution
Step 1: Understand the purpose of add_categories()
This method is used to add new possible categories to a pandas categorical column without changing existing data.
Step 2: Differentiate from other methods
Unlike remove_categories() which removes categories, add_categories() only adds new ones.
Final Answer:
It adds new possible categories to an existing categorical column. -> Option C
Quick Check:
add_categories() adds categories [OK]
Hint: Remember: add_categories adds new options, doesn't remove [OK]
Common Mistakes:
Confusing add_categories with remove_categories
Thinking it changes existing data values
Assuming it renames categories
2. Which of the following is the correct syntax to remove categories from a pandas categorical column named df['color']?
easy
A. df['color'].remove_categories(['red', 'blue'])
B. df['color'].drop_categories(['red', 'blue'])
C. df['color'].delete_categories(['red', 'blue'])
D. df['color'].pop_categories(['red', 'blue'])
Solution
Step 1: Recall the correct method name
The pandas method to remove categories is remove_categories().
Step 2: Match the syntax with the method
df['color'].remove_categories(['red', 'blue']) uses remove_categories() correctly on the categorical column.
Final Answer:
df['color'].remove_categories(['red', 'blue']) -> Option A
Quick Check:
Use remove_categories() to remove categories [OK]
Hint: Remember method name is remove_categories, not drop or delete [OK]
A. Categorical cannot have more than 3 categories.
B. add_categories cannot add a single category.
C. You cannot chain add_categories and remove_categories.
D. remove_categories expects a list, not a string.
Solution
Step 1: Check method argument types
remove_categories() requires a list or iterable of categories to remove, not a single string.
Step 2: Identify the error cause
Passing 'cat' as a string causes a TypeError or unexpected behavior.
Final Answer:
remove_categories expects a list, not a string. -> Option D
Quick Check:
Pass list to remove_categories [OK]
Hint: Always pass list to remove_categories, not single string [OK]
Common Mistakes:
Passing single string instead of list to remove_categories
Thinking add_categories can't add one item
Assuming chaining methods is invalid
5. You have a pandas categorical column df['status'] with categories ['new', 'in_progress', 'done']. You want to add a new category 'archived' and remove 'new' without losing data for other rows. Which sequence of commands achieves this correctly?
hard
A. df['status'] = df['status'].remove_categories(['new'])
df['status'].add_categories(['archived'])
B. df['status'] = df['status'].add_categories(['archived'])
df['status'] = df['status'].remove_categories(['new'])
C. df['status'].remove_categories(['new'])
df['status'].add_categories(['archived'])
D. df['status'].add_categories('archived')
df['status'].remove_categories('new')
Solution
Step 1: Add new category before removing
Adding 'archived' first ensures it exists as a valid category before removal.
Step 2: Remove 'new' category properly
Removing 'new' replaces its values with NaN, so do this after adding new category and assign back to column.
Step 3: Assign results back to the column
Both methods return new categorical objects, so assign back to df['status'].
Final Answer:
df['status'] = df['status'].add_categories(['archived'])
df['status'] = df['status'].remove_categories(['new']) -> Option B
Quick Check:
Add then remove, assign back [OK]
Hint: Add categories first, then remove, assign back [OK]
Common Mistakes:
Removing category before adding new one
Not assigning back to the DataFrame column
Passing string instead of list to methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.