pandas 1.4.2

ParametersReturns
map(self, mapper)

Maps the categories to new categories. If the mapping correspondence is one-to-one the result is a ~pandas.Categorical which has the same order property as the original, otherwise a ~pandas.Index is returned. NaN values are unaffected.

If a :None:None:`dict` or ~pandas.Series is used any unmapped category is mapped to NaN . Note that if this happens an ~pandas.Index will be returned.

Parameters

mapper : function, dict, or Series

Mapping correspondence.

Returns

pandas.Categorical or pandas.Index

Mapped categorical.

Map categories using an input mapping or function.

See Also

CategoricalIndex.map

Apply a mapping correspondence on a :None:class:`~pandas.CategoricalIndex`.

Index.map

Apply a mapping correspondence on an :None:class:`~pandas.Index`.

Series.apply

Apply more complex functions on a :None:class:`~pandas.Series`.

Series.map

Apply a mapping correspondence on a :None:class:`~pandas.Series`.

Examples

This example is valid syntax, but we were not able to check execution
>>> cat = pd.Categorical(['a', 'b', 'c'])
... cat ['a', 'b', 'c'] Categories (3, object): ['a', 'b', 'c']
This example is valid syntax, but we were not able to check execution
>>> cat.map(lambda x: x.upper())
['A', 'B', 'C']
Categories (3, object): ['A', 'B', 'C']
This example is valid syntax, but we were not able to check execution
>>> cat.map({'a': 'first', 'b': 'second', 'c': 'third'})
['first', 'second', 'third']
Categories (3, object): ['first', 'second', 'third']

If the mapping is one-to-one the ordering of the categories is preserved:

This example is valid syntax, but we were not able to check execution
>>> cat = pd.Categorical(['a', 'b', 'c'], ordered=True)
... cat ['a', 'b', 'c'] Categories (3, object): ['a' < 'b' < 'c']
This example is valid syntax, but we were not able to check execution
>>> cat.map({'a': 3, 'b': 2, 'c': 1})
[3, 2, 1]
Categories (3, int64): [3 < 2 < 1]

If the mapping is not one-to-one an ~pandas.Index is returned:

This example is valid syntax, but we were not able to check execution
>>> cat.map({'a': 'first', 'b': 'second', 'c': 'first'})
Index(['first', 'second', 'first'], dtype='object')

If a :None:None:`dict` is used, all unmapped categories are mapped to NaN and the result is an ~pandas.Index :

This example is valid syntax, but we were not able to check execution
>>> cat.map({'a': 'first', 'b': 'second'})
Index(['first', 'second', nan], dtype='object')
See :

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/arrays/categorical.py#1361
type: <class 'function'>
Commit: