pandas.Series.cat.remove_unused_categories#

Series.cat.remove_unused_categories()[source]#

Remove categories which are not used.

This method is useful when working with datasets that undergo dynamic changes where categories may no longer be relevant, allowing to maintain a clean, efficient data structure.

Returns:
Categorical

Categorical with unused categories dropped.

See also

rename_categories

Rename categories.

reorder_categories

Reorder categories.

add_categories

Add new categories.

remove_categories

Remove the specified categories.

set_categories

Set the categories to the specified ones.

Examples

>>> c = pd.Categorical(["a", "c", "b", "c", "d"])
>>> c
['a', 'c', 'b', 'c', 'd']
Categories (4, object): ['a', 'b', 'c', 'd']
>>> c[2] = "a"
>>> c[4] = "c"
>>> c
['a', 'c', 'a', 'c', 'c']
Categories (4, object): ['a', 'b', 'c', 'd']
>>> c.remove_unused_categories()
['a', 'c', 'a', 'c', 'c']
Categories (2, object): ['a', 'c']