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
Python Pandas CategoricalIndex - Rename categories with dict-like new categories
To rename categories with dict-like new categories, use the CategoricalIndex rename_categories() method in Pandas. This method allows you to map old category names to new ones using a dictionary.
What is CategoricalIndex?
CategoricalIndex can only take on a limited, and usually fixed, number of possible values. It's useful for representing data with a finite set of categories.
Creating a CategoricalIndex
First, let's create a CategoricalIndex with some sample data ?
import pandas as pd
# Create CategoricalIndex with ordered categories
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"])
print("Original CategoricalIndex:")
print(catIndex)
print("\nCategories:")
print(catIndex.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')
Renaming Categories with Dictionary
Use rename_categories() with a dictionary to map old category names to new ones ?
import pandas as pd
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"])
# Rename categories using dictionary mapping
renamed_catIndex = catIndex.rename_categories({'p': 5, 'q': 10, 'r': 15, 's': 20})
print("CategoricalIndex after renaming categories:")
print(renamed_catIndex)
print("\nNew categories:")
print(renamed_catIndex.categories)
CategoricalIndex after renaming categories: CategoricalIndex([5, 10, 15, 20, 5, 10, 15, 20], categories=[5, 10, 15, 20], ordered=True, dtype='category') New categories: Index([5, 10, 15, 20], dtype='int64')
Practical Example
Here's a more practical example renaming size categories ?
import pandas as pd
# Create size categories
sizes = pd.CategoricalIndex(["S", "M", "L", "XL", "S", "M"],
categories=["S", "M", "L", "XL"])
print("Original sizes:")
print(sizes)
# Rename to full words
full_names = sizes.rename_categories({
'S': 'Small',
'M': 'Medium',
'L': 'Large',
'XL': 'Extra Large'
})
print("\nRenamed sizes:")
print(full_names)
Original sizes: CategoricalIndex(['S', 'M', 'L', 'XL', 'S', 'M'], categories=['S', 'M', 'L', 'XL'], ordered=False, dtype='category') Renamed sizes: CategoricalIndex(['Small', 'Medium', 'Large', 'Extra Large', 'Small', 'Medium'], categories=['Small', 'Medium', 'Large', 'Extra Large'], ordered=False, dtype='category')
Conclusion
The rename_categories() method with dictionary mapping provides a clean way to rename categorical values. It maintains the structure and ordering while updating category names to more meaningful labels.
