Pandas Tutorial Pandas References

Pandas Series - map() function



The Pandas Series map() function is used for substituting each value in a Series with another value, that may be derived from a function, a dictionary or a Series.

Syntax

Series.map(arg, na_action=None)

Parameters

arg Optional. Specify mapping correspondence. It can be a function, a dictionary or a Series.
na_action Optional. If set to 'ignore', propagate NaN values, without passing them to the mapping correspondence. It can take value from {None, 'ignore'}. Default is None.

Return Value

Returns Series with the same index as a caller.

Example: map() example

In the example below, the map() function is used to substitute each value in the given Series with value specified in the given dictionary argument.

import pandas as pd
import numpy as np

x = pd.Series(['UK', 'India', 'USA', 'Germany', np.NaN])

print("The Series contains:")
print(x, "\n")

#using dict to substitute each 
#value in the given Series
print("x.map({'UK': 'England', 'USA': 'America'}) returns:")
print(x.map({'UK': 'England', 'USA': 'America'}),"\n")

The output of the above code will be:

The Series contains:
0         UK
1      India
2        USA
3    Germany
4        NaN
dtype: object 

x.map({'UK': 'England', 'USA': 'America'}) returns:
0    England
1        NaN
2    America
3        NaN
4        NaN
dtype: object 

Example: using function argument

It also accepts a function as argument. Consider the example below:

import pandas as pd
import numpy as np

x = pd.Series(['UK', 'India', 'USA', 'Germany', np.NaN])

print("The Series contains:")
print(x, "\n")

#using function as argument
print("x.map('I love {}'.format) returns:")
print(x.map('I love {}'.format),"\n")

The output of the above code will be:

The Series contains:
0         UK
1      India
2        USA
3    Germany
4        NaN
dtype: object 

x.map('I love {}'.format) returns:
0         I love UK
1      I love India
2        I love USA
3    I love Germany
4        I love nan
dtype: object 

Example: using na_action parameter

By setting na_action parameter to 'ignore', this function propagate NaN values, without passing them to the mapping correspondence. Consider the example below:

import pandas as pd
import numpy as np

x = pd.Series(['UK', 'India', 'USA', 'Germany', np.NaN])

print("The Series contains:")
print(x, "\n")

#using function as argument
print("x.map('I love {}'.format) returns:")
print(x.map('I love {}'.format),"\n")

#using function as argument with na_action
print("x.map('I love {}'.format, 'ignore') returns:")
print(x.map('I love {}'.format, 'ignore'),"\n")

The output of the above code will be:

The Series contains:
0         UK
1      India
2        USA
3    Germany
4        NaN
dtype: object 

x.map('I love {}'.format) returns:
0         I love UK
1      I love India
2        I love USA
3    I love Germany
4        I love nan
dtype: object 

x.map('I love {}'.format, 'ignore') returns:
0         I love UK
1      I love India
2        I love USA
3    I love Germany
4               NaN
dtype: object 

❮ Pandas Series - Functions

5