
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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
- 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 - Return a components namedtuple-like
- Python Pandas CategoricalIndex - Check whether the categories have an ordered relationship
- Python Pandas - Set the categories of the CategoricalIndex to be unordered

Advertisements