- API reference
- pandas.CategoricalIndex
- pandas.Categ...
pandas.CategoricalIndex.rename_categories#
- CategoricalIndex.rename_categories(*args,**kwargs)[source]#
Rename categories.
- Parameters:
- new_categorieslist-like, dict-like or callable
New categories which will replace old categories.
list-like: all items must be unique and the number of items inthe new categories must match the existing number of categories.
dict-like: specifies a mapping fromold categories to new. Categories not contained in the mappingare passed through and extra categories in the mapping areignored.
callable : a callable that is called on all items in the oldcategories and whose return values comprise the new categories.
- Returns:
- Categorical
Categorical with renamed categories.
- Raises:
- ValueError
If new categories are list-like and do not have the same number ofitems than the current categories or do not validate as categories
See also
reorder_categoriesReorder categories.
add_categoriesAdd new categories.
remove_categoriesRemove the specified categories.
remove_unused_categoriesRemove categories which are not used.
set_categoriesSet the categories to the specified ones.
Examples
>>>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 andcategories not in the dictionary are passed through>>>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
>>>c.rename_categories(lambdax:x.upper())['A', 'A', 'B']Categories (2, object): ['A', 'B']