Bird
Raised Fist0
Pandasdata~10 mins

Adding and removing categories in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Adding and removing categories
Start with Categorical Data
↓
Add new category
↓
Check categories updated?
No→Error or Retry
Yes↓
Remove category
↓
Check categories updated?
No→Error or Retry
Yes↓
End
Start with categorical data, add new categories, verify update, then remove categories and verify again.
Execution Sample
Pandas
import pandas as pd
cat = pd.Categorical(['a', 'b', 'a'], categories=['a', 'b'])
cat = cat.add_categories(['c'])
cat = cat.remove_categories(['b'])
print(cat.categories)
Create a categorical variable, add a new category 'c', remove category 'b', then print remaining categories.
Execution Table
StepActionCategories BeforeCategories AfterNotes
1Create categorical with categories ['a', 'b'][]['a', 'b']Initial categories set
2Add category 'c'['a', 'b']['a', 'b', 'c']New category 'c' added
3Remove category 'b'['a', 'b', 'c']['a', 'c']Category 'b' removed
4Print categories['a', 'c']['a', 'c']Final categories displayed
5End['a', 'c']['a', 'c']No more steps
💡 All categories updated correctly; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
cat.categories[]['a', 'b']['a', 'b', 'c']['a', 'c']['a', 'c']
Key Moments - 2 Insights
Why does the category list change after adding a new category?
Because add_categories adds the new category to the existing list without changing existing data, as shown in step 2 of the execution_table.
What happens to data points with a removed category?
Data points with the removed category become NaN or missing, since the category no longer exists, as implied after step 3 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what are the categories after step 2?
A['a', 'c']
B['a', 'b']
C['a', 'b', 'c']
D['b', 'c']
💡 Hint
Check the 'Categories After' column for step 2 in the execution_table.
At which step is the category 'b' removed?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table to find when 'b' is removed.
If we add category 'd' after step 3, what will be the categories?
A['a', 'c', 'd']
B['a', 'b', 'c', 'd']
C['a', 'b', 'd']
D['a', 'c']
💡 Hint
After step 3, categories are ['a', 'c'], adding 'd' appends it.
Concept Snapshot
pandas Categorical categories can be changed.
Use add_categories(['new_cat']) to add.
Use remove_categories(['old_cat']) to remove.
Removing a category affects data points with it.
Always check categories after changes.
Full Transcript
We start with a pandas Categorical variable with categories 'a' and 'b'. Then we add a new category 'c' using add_categories, which updates the categories list to include 'c'. Next, we remove the category 'b' using remove_categories, which removes 'b' from the categories list. Finally, we print the categories to see the updated list, which is now ['a', 'c']. This process shows how to add and remove categories step-by-step in pandas.

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

  1. 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.
  2. Step 2: Differentiate from other methods

    Unlike remove_categories() which removes categories, add_categories() only adds new ones.
  3. Final Answer:

    It adds new possible categories to an existing categorical column. -> Option C
  4. 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

  1. Step 1: Recall the correct method name

    The pandas method to remove categories is remove_categories().
  2. Step 2: Match the syntax with the method

    df['color'].remove_categories(['red', 'blue']) uses remove_categories() correctly on the categorical column.
  3. Final Answer:

    df['color'].remove_categories(['red', 'blue']) -> Option A
  4. Quick Check:

    Use remove_categories() to remove categories [OK]
Hint: Remember method name is remove_categories, not drop or delete [OK]
Common Mistakes:
  • Using non-existent methods like drop_categories
  • Confusing remove_categories with add_categories
  • Trying to remove categories with pop or delete
3. Given the code:
import pandas as pd
cat = pd.Categorical(['apple', 'banana', 'apple'], categories=['apple', 'banana', 'cherry'])
cat2 = cat.remove_categories(['banana'])
print(cat2)

What will be the output?
medium
A. [apple, NaN, apple] Categories (2, object): [apple, cherry]
B. [apple, banana, apple] Categories (2, object): [apple, cherry]
C. [apple, NaN, apple] Categories (3, object): [apple, banana, cherry]
D. [apple, banana, apple] Categories (3, object): [apple, banana, cherry]

Solution

  1. Step 1: Understand remove_categories effect on data

    Removing 'banana' category replaces all 'banana' values with NaN and removes 'banana' from categories.
  2. Step 2: Analyze the printed output

    Values become ['apple', NaN, 'apple'] and categories become ['apple', 'cherry'] with length 2.
  3. Final Answer:

    [apple, NaN, apple] Categories (2, object): [apple, cherry] -> Option A
  4. Quick Check:

    Removed category replaced by NaN [OK]
Hint: Removing category replaces values with NaN [OK]
Common Mistakes:
  • Expecting removed category values to stay unchanged
  • Thinking categories length remains same
  • Confusing categories list after removal
4. Consider this code snippet:
import pandas as pd
cat = pd.Categorical(['dog', 'cat', 'bird'], categories=['dog', 'cat', 'bird'])
cat = cat.add_categories('fish')
cat = cat.remove_categories('cat')

What is the error in this code?
medium
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

  1. Step 1: Check method argument types

    remove_categories() requires a list or iterable of categories to remove, not a single string.
  2. Step 2: Identify the error cause

    Passing 'cat' as a string causes a TypeError or unexpected behavior.
  3. Final Answer:

    remove_categories expects a list, not a string. -> Option D
  4. 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

  1. Step 1: Add new category before removing

    Adding 'archived' first ensures it exists as a valid category before removal.
  2. 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.
  3. Step 3: Assign results back to the column

    Both methods return new categorical objects, so assign back to df['status'].
  4. Final Answer:

    df['status'] = df['status'].add_categories(['archived']) df['status'] = df['status'].remove_categories(['new']) -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard