pandas 1.4.2

NotesParametersReturnsBackRef
map(self, arg, na_action=None) -> 'Series'

Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series .

Notes

When arg is a dictionary, values in Series that are not in the dictionary (as keys) are converted to NaN . However, if the dictionary is a dict subclass that defines __missing__ (i.e. provides a method for default values), then this default is used rather than NaN .

Parameters

arg : function, collections.abc.Mapping subclass or Series

Mapping correspondence.

na_action : {None, 'ignore'}, default None

If 'ignore', propagate NaN values, without passing them to the mapping correspondence.

Returns

Series

Same index as caller.

Map values of Series according to an input mapping or function.

See Also

DataFrame.apply

Apply a function row-/column-wise.

DataFrame.applymap

Apply a function elementwise on a whole DataFrame.

Series.apply

For applying more complex functions on a Series.

Examples

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(['cat', 'dog', np.nan, 'rabbit'])
... s 0 cat 1 dog 2 NaN 3 rabbit 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 ):

This example is valid syntax, but we were not able to check execution
>>> s.map({'cat': 'kitten', 'dog': 'puppy'})
0   kitten
1    puppy
2      NaN
3      NaN
dtype: object

It also accepts a function:

This example is valid syntax, but we were not able to check execution
>>> s.map('I am a {}'.format)
0       I am a cat
1       I am a dog
2       I am a nan
3    I am a rabbit
dtype: object

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

This example is valid syntax, but we were not able to check execution
>>> s.map('I am a {}'.format, na_action='ignore')
0     I am a cat
1     I am a dog
2            NaN
3  I am a rabbit
dtype: object
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.arrays.categorical.Categorical.map pandas.core.indexes.category.CategoricalIndex.map pandas.core.series.Series.apply pandas.core.generic.NDFrame.pipe

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/series.py#4162
type: <class 'function'>
Commit: