pyspark.pandas.CategoricalIndex.reorder_categories

CategoricalIndex.reorder_categories(new_categories: Union[pandas.core.indexes.base.Index, Any, List], ordered: Optional[bool] = None, inplace: bool = False) → Optional[pyspark.pandas.indexes.category.CategoricalIndex][source]

Reorder categories as specified in new_categories.

new_categories needs to include all old categories and no new category items.

Parameters
new_categoriesIndex-like

The categories in new order.

orderedbool, optional

Whether or not the categorical is treated as an ordered categorical. If not given, do not change the ordered information.

inplacebool, default False

Whether or not to reorder the categories inplace or return a copy of this categorical with reordered categories.

Deprecated since version 3.2.0.

Returns
catCategoricalIndex or None

Categorical with removed categories or None if inplace=True.

Raises
ValueError

If the new categories do not contain all old category items or any new ones

See also

rename_categories

Rename categories.

add_categories

Add new categories.

remove_categories

Remove the specified categories.

remove_unused_categories

Remove categories which are not used.

set_categories

Set the categories to the specified ones.

Examples

>>> idx = ps.CategoricalIndex(list("abbccc"))
>>> idx  
CategoricalIndex(['a', 'b', 'b', 'c', 'c', 'c'],
                 categories=['a', 'b', 'c'], ordered=False, dtype='category')
>>> idx.reorder_categories(['c', 'b', 'a'])  
CategoricalIndex(['a', 'b', 'b', 'c', 'c', 'c'],
                 categories=['c', 'b', 'a'], ordered=False, dtype='category')