- API reference
- Series
- pandas.Series.map
pandas.Series.map#
- Series.map(arg,na_action=None)[source]#
Map values of Series according to an input mapping or function.
Used for substituting each value in a Series with another value,that may be derived from a function, a
dict
oraSeries
.- Parameters:
- argfunction, collections.abc.Mapping subclass or Series
Mapping correspondence.
- na_action{None, ‘ignore’}, default None
If ‘ignore’, propagate NaN values, without passing them to themapping correspondence.
- Returns:
- Series
Same index as caller.
See also
Series.apply
For applying more complex functions on a Series.
Series.replace
Replace values given into_replace withvalue.
DataFrame.apply
Apply a function row-/column-wise.
DataFrame.map
Apply a function elementwise on a whole DataFrame.
Notes
When
arg
is a dictionary, values in Series that are not in thedictionary (as keys) are converted toNaN
. However, if thedictionary is adict
subclass that defines__missing__
(i.e.provides a method for default values), then this default is usedrather thanNaN
.Examples
>>>s=pd.Series(['cat','dog',np.nan,'rabbit'])>>>s0 cat1 dog2 NaN3 rabbitdtype: object
map
accepts adict
or aSeries
. Values that are not foundin thedict
are converted toNaN
, unless the dict has a defaultvalue (e.g.defaultdict
):>>>s.map({'cat':'kitten','dog':'puppy'})0 kitten1 puppy2 NaN3 NaNdtype: object
It also accepts a function:
>>>s.map('I am a{}'.format)0 I am a cat1 I am a dog2 I am a nan3 I am a rabbitdtype: object
To avoid applying the function to missing values (and keep them as
NaN
)na_action='ignore'
can be used:>>>s.map('I am a{}'.format,na_action='ignore')0 I am a cat1 I am a dog2 NaN3 I am a rabbitdtype: object