pandas.CategoricalIndex.remove_unused_categories#

CategoricalIndex.remove_unused_categories(*args, **kwargs)[源代码]#

删除未使用的类别。

Returns:
Categorical

已移除未使用类别的 Categorical。

参见

rename_categories

重命名类别。

reorder_categories

重新排序类别。

add_categories

添加新类别。

remove_categories

删除指定的类别。

set_categories

将类别设置为指定的类别。

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']