Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
s = pd.Series(['fox', 'cow', np.nan, 'dog'])
s
Out[2]:
0    fox
1    cow
2    NaN
3    dog
dtype: object

map accepts a dict or a Series. Values that are not found in the dict are converted to NaN, unless
the dict has a default value (e.g. defaultdict):

In [3]:
s.map({'fox': 'cub', 'cow': 'calf'})
Out[3]:
0     cub
1    calf
2     NaN
3     NaN
dtype: object

It also accepts a function:

In [4]:
s.map('I am a {}'.format)
Out[4]:
0    I am a fox
1    I am a cow
2    I am a nan
3    I am a dog
dtype: object

To avoid applying the function to missing values (and keep them as NaN) na_action='ignore' can be used:

In [5]:
s.map('I am a {}'.format, na_action='ignore')
Out[5]:
0    I am a fox
1    I am a cow
2           NaN
3    I am a dog
dtype: object