pandas 1.4.2

ParametersRaisesReturnsBackRef
rename_categories(self, new_categories, inplace=<no_default>)

Parameters

new_categories : list-like, dict-like or callable

New categories which will replace old categories.

  • list-like: all items must be unique and the number of items in the new categories must match the existing number of categories.

  • dict-like: specifies a mapping from old categories to new. Categories not contained in the mapping are passed through and extra categories in the mapping are ignored.

inplace : bool, default False

Whether or not to rename the categories inplace or return a copy of this categorical with renamed categories.

deprecated

Raises

ValueError

If new categories are list-like and do not have the same number of items than the current categories or do not validate as categories

Returns

cat : Categorical or None

Categorical with removed categories or None if inplace=True .

Rename categories.

See Also

add_categories

Add new categories.

remove_categories

Remove the specified categories.

remove_unused_categories

Remove categories which are not used.

reorder_categories

Reorder categories.

set_categories

Set the categories to the specified ones.

Examples

This example is valid syntax, but we were not able to check execution
>>> c = pd.Categorical(['a', 'a', 'b'])
... c.rename_categories([0, 1]) [0, 0, 1] Categories (2, int64): [0, 1]

For dict-like new_categories , extra keys are ignored and categories not in the dictionary are passed through

This example is valid syntax, but we were not able to check execution
>>> c.rename_categories({'a': 'A', 'c': 'C'})
['A', 'A', 'b']
Categories (2, object): ['A', 'b']

You may also provide a callable to create the new categories

This example is valid syntax, but we were not able to check execution
>>> c.rename_categories(lambda x: x.upper())
['A', 'A', 'B']
Categories (2, object): ['A', 'B']
See :

Back References

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

pandas.core.arrays.categorical.Categorical.set_categories pandas.core.arrays.categorical.Categorical.remove_categories pandas.core.arrays.categorical.Categorical.add_categories pandas.core.arrays.categorical.Categorical.remove_unused_categories pandas.core.arrays.categorical.Categorical.reorder_categories

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#983
type: <class 'function'>
Commit: