Python: Functions-map()

Rahul S
2 min readSep 13, 2023

The map()transform values in a Series or DataFrame based on a mapping or function. With it, we can apply custom transformations to our data.

Syntax:, na_action=None)
  • arg: This parameter can take one of the following:
  1. A dictionary: It maps current values to new values.

2. A Series: It uses the Series values to perform the mapping.

3. A function: It applies the function to each element in the Series.

  • na_action (optional): This parameter specifies what to do with missing values (NaNs). It can be one of 'ignore' (default), 'raise', or None.

Mapping Values Using a Dictionary:

import pandas as pd
data = {'A':['apple','banana','cherry','apple']}
fruit_to_color= {'apple':'red', 'banana':'yellow', 'cherry':'red'}

This code maps fruit names in the ‘A’ column to their corresponding colors using a dictionary and creates a new ‘Color’ column in the DataFrame.

Mapping Values Using a Series:

import pandas as pd
data = {'A':['apple','banana','cherry','apple']}
color_series=pd.Series(['red','yellow','red'], index=['apple','banana','cherry'])

Here, a Series color_series is used to map fruit names to colors, similar to the dictionary approach.

Applying a Function:

import pandas as pd  
data = {'Numbers': [1, 2, 3, 4]}
df = pd.DataFrame(data)
def square(x):
return x ** 2
df['Squared'] = df['Numbers'].map(square)

Here, a custom function square() is applied to each value in the 'Numbers' column to create a new 'Squared' column with squared values.

Handling Missing Values:

import pandas as pd 
import numpy as np
data = {'A': [1, 2, np.nan, 4]}
df = pd.DataFrame(data)
def custom_function(x):
return x * 2 if not pd.isna(x) else x
df['Doubled'] = df['A'].map(custom_function)

This example demonstrates how to handle missing values (NaNs) using a custom function.

The map() function is particularly useful when you need to apply a transformation to individual elements in a Series or DataFrame column. It provides flexibility and allows you to customize the mapping logic based on your specific data processing requirements.