0
0
Pandasdata~30 mins

Adding and removing categories in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Adding and Removing Categories in pandas
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(products.cat.categories) to see the current categories.