Python Pandas - Remove the specified categories from CategoricalIndex



To remove the specified categories from CategoricalIndex, use the remove_categories() method in Pandas.

At first, import the required libraries −

import pandas as pd

Set the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −

catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])

Remove categories using remove_categories(). Set the categories to be removed as a parameter. Values which were in the removed categories will be set to NaN −

print("\nCategoricalIndex after removing specified categories...\n", catIndex.remove_categories(["p", "q"]))

Example

Following is the code −

import pandas as pd # Set the categories for the categorical using the "categories" parameter # Treat the categorical as ordered using the "ordered" parameter catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) # Display the CategoricalIndex print("CategoricalIndex...\n",catIndex) # Get the categories print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories) # Remove categories using remove_categories() # Set the categories to be removed as a parameter # Values which were in the removed categories will be set to NaN print("\nCategoricalIndex after removing specified categories...\n", catIndex.remove_categories(["p", "q"]))

Output

This will produce the following output −

CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex after removing specified categories... CategoricalIndex([nan, nan, 'r', 's', nan, nan, 'r', 's'], categories=['r', 's'], ordered=True, dtype='category')
Updated on: 2021-10-18T07:10:02+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements