map function in Python Pandas – Primarily used for transforming values in a Series

Python Pandas @ Freshers.in

The map function is a versatile tool for transforming data. This article delves into the nuances of the map function, providing practical insights and a real-world example to enhance your data manipulation skills. The map function in pandas offers an efficient and flexible way to transform data.

Understanding the map function in Pandas

The map function in pandas is primarily used for transforming values in a Series. It accepts a function or a dictionary with mappings and applies it to each element in the Series. This function is particularly useful for substituting values, deriving new information, or formatting existing data.

Key features:

  • Element-wise transformation: Applies a function to each element individually.
  • Flexibility: Can use a function, dictionary, or a Series.
  • Efficiency: Optimized for performance in pandas.

Example: Employee data transformation

In this example, we will transform employee names to their respective roles in a company.

Data Setup:

Imagine we have a pandas DataFrame containing employee names and their roles:

import pandas as pd
# Sample data
data = {'Name': ['Sachin', 'Ram', 'Raju', 'David'],
        'Role': ['Manager', 'Analyst', 'Clerk', 'Director']}
df = pd.DataFrame(data)
Transformation Objective:

Our objective is to create a new column ‘Role_Code’ that maps each role to a specific code.

Step-by-Step Guide:

Create a mapping dictionary: First, we define a dictionary that maps each role to a unique code.

role_map = {'Manager': 'MGR', 'Analyst': 'ANL', 'Clerk': 'CLK', 'Director': 'DIR'}

Apply the Map Function: We use the map function to transform the ‘Role’ column.

df['Role_Code'] = df['Role'].map(role_map)
Result: The DataFrame now has a new column with the transformed data.
print(df)
   Name      Role Role_Code
0  Sachin   Manager       MGR
1     Ram   Analyst       ANL
2    Raju     Clerk       CLK
3   David  Director       DIR
Author: user