Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Remove the specified categories from CategoricalIndex
To remove the specified categories from CategoricalIndex, use the remove_categories() method in Pandas. This method removes categories from the index and sets values that were in the removed categories to NaN.
Creating a CategoricalIndex
First, let's create a CategoricalIndex with some categories ?
import pandas as pd
# Create CategoricalIndex with categories p, q, r, s
cat_index = pd.CategoricalIndex(
["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"]
)
print("Original CategoricalIndex:")
print(cat_index)
print("\nCategories:")
print(cat_index.categories)
Original CategoricalIndex: CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') Categories: Index(['p', 'q', 'r', 's'], dtype='object')
Removing Categories
Use remove_categories() to remove specific categories. Values that belonged to the removed categories will become NaN ?
import pandas as pd
cat_index = pd.CategoricalIndex(
["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"]
)
# Remove categories 'p' and 'q'
updated_index = cat_index.remove_categories(["p", "q"])
print("CategoricalIndex after removing 'p' and 'q':")
print(updated_index)
print("\nRemaining categories:")
print(updated_index.categories)
CategoricalIndex after removing 'p' and 'q': CategoricalIndex([nan, nan, 'r', 's', nan, nan, 'r', 's'], categories=['r', 's'], ordered=True, dtype='category') Remaining categories: Index(['r', 's'], dtype='object')
Key Points
- The
remove_categories()method permanently removes categories from the CategoricalIndex - Values that were in the removed categories are automatically set to NaN
- The method returns a new CategoricalIndex object with the updated categories
- The order of remaining categories is preserved if the original index was ordered
Conclusion
The remove_categories() method provides a clean way to remove unwanted categories from a CategoricalIndex. Remember that values in the removed categories will become NaN, so handle missing data appropriately in your analysis.
Advertisements
