

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas CategoricalIndex - Map values using input correspondence like a dict
To Map values using input correspondence like a dict, use the CategoricalIndex.map() 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"])
Display the CategoricalIndex −
print("CategoricalIndex...\n",catIndex)
Map categories −
print("\nCategoricalIndex after mapping...\n",catIndex.map({'P': 5, 'Q': 10, 'R': 15, 'S': 20}))
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("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories) # Map categories print("\nCategoricalIndex after mapping...\n",catIndex.map({'P': 5, 'Q': 10, 'R': 15, 'S': 20}))
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') DisplayingCategories from CategoricalIndex... Index(['P', 'Q', 'R', 'S'], dtype='object') CategoricalIndex after mapping... CategoricalIndex([5, 10, 15, 20, 5, 10, 15, 20], categories=[5, 10, 15, 20], ordered=True, dtype='category')
- Related Questions & Answers
- Python Pandas CategoricalIndex - Rename categories with dict-like new categories
- Python Pandas CategoricalIndex - Rename categories
- Python Pandas CategoricalIndex - Reorder categories
- Python - Convert a list of lists into tree-like dict
- Python Pandas CategoricalIndex - Add new categories
- Python Pandas CategoricalIndex - Rename categories with lambda
- Python Pandas - Remove the specified categories from CategoricalIndex
- Python Pandas - Return a components namedtuple-like
- Create a Pandas Dataframe from a dict of equal length lists in Python
- Python Pandas - Get the minimum value from Ordered CategoricalIndex
- Python Pandas - Get the maximum value from Ordered CategoricalIndex
- Python Pandas CategoricalIndex - Get the categories of this categorical
- Python Pandas CategoricalIndex - Get the category codes of this categorical
- Python Pandas CategoricalIndex - Check whether the categories have an ordered relationship
- Python Pandas - Set the categories of the CategoricalIndex to be unordered
Advertisements