Pandas map()

The map() method in Pandas is used to substitute values in a Series with another value.

Example

import pandas as pd

# create a Series 
series = pd.Series([1, 2, 3])

# dictionary (mapping correspondence)
mapping_dict = {1: 'one', 2: 'two', 3: 'three'}

# substitute Series values using the dictionary mapped_series = series.map(mapping_dict)
print(mapped_series) ''' Output 0 one 1 two 2 three dtype: object '''

map() Syntax

The syntax of the map() method in Pandas is:

Series.map(arg, na_action=None)

map() Arguments

The map() method takes following arguments:

  • arg - mapping correspondence, which can be a function, dictionary, or another Series that defines how the values in the Series should be transformed
  • na_action (optional) - if set to ignore, missing values in the Series will not be passed to the mapping function and will remain as NaN in the output.

map() Return Value

The map() method returns a Series where each element is the result of applying the mapping argument to each element of the original Series.


Example 1: Use a Dictionary for Mapping

import pandas as pd

# create a Series with fruit names
fruits = pd.Series(['apple', 'banana', 'cherry', 'date'])

# dictionary mapping fruit names to their colors
fruit_colors = {
    'apple': 'red',
    'banana': 'yellow',
    'cherry': 'red',
    'date': 'brown'
}

# use map() to transform the fruit names into their colors colors = fruits.map(fruit_colors)
print(colors)

Output

0       red
1    yellow
2       red
3     brown
dtype: object

In this example, the map() method is used to substitute each value in the fruits Series with the value from the fruit_colors dictionary.


Example 2: Use a Function for Mapping

import pandas as pd

# create a Series 
series = pd.Series([1, 2, 3, 4, 5])

# use map() with a lambda function to square each value in the series mapped_series = series.map(lambda x: x**2)
print(mapped_series)

Output

0     1
1     4
2     9
3    16
4    25
dtype: int64

In the above example, the map() method is used with the lambda function lambda x: x**2 that takes each element x from the Series and returns its square.

The resulting series is replaced by the square of the original series.


Example 3: Handling Missing Values with na_action Argument in map()

The na_action argument in the map() method is used to control the behavior of the function when it encounters missing values in the Series.

  1. If na_action=None (default) - the mapping is applied to missing values as well
  2. If na_action='ignore' - any missing values in the Series are automatically bypassed or ignored by the map() method

Let's look at an example.

import pandas as pd

# create a Series with a missing value
series = pd.Series([1, 2, None, 4])

# function to increment a number, but convert missing value to 0
def increment_or_zero(x):
    if pd.isna(x):
        return 0
    else:
        return x + 1

# apply map without ignoring NaN result_without_ignore = series.map(increment_or_zero)
print("Without ignore:\n", result_without_ignore) print()
# apply map with ignore result_with_ignore = series.map(increment_or_zero, na_action='ignore')
print("With ignore:\n", result_with_ignore)

Output

Without ignore:
0    2.0
1    3.0
2    0.0
3    5.0
dtype: float64

With ignore:
0    2.0
1    3.0
2    NaN
3    5.0
dtype: float64

Here,

  • Without na_action='ignore' - increment_or_zero() is applied to all values, including missing values. Therefore, the missing value in the series is converted to 0.
  • With na_action='ignore' - missing values are ignored by increment_or_zero(), so increment_or_zero() is not applied to missing values.