pandas.Categorical.categories#

property Categorical.categories[源代码]#

此分类的分类。

设置会为每个分类分配新值(有效地重命名每个单独的分类)。

指定的值必须是类似列表的对象。所有项必须是唯一的,并且新分类中的项数必须与旧分类中的项数相同。

Raises:
ValueError

如果新类别无法验证为类别,或者新类别的数量与旧类别数量不相等

参见

rename_categories

重命名类别。

reorder_categories

重新排序类别。

add_categories

添加新类别。

remove_categories

删除指定的类别。

remove_unused_categories

删除未使用的类别。

set_categories

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

Examples

对于 pandas.Series

>>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
>>> ser.cat.categories
Index(['a', 'b', 'c'], dtype='object')
>>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], categories=['b', 'c', 'd'])
>>> ser = pd.Series(raw_cat)
>>> ser.cat.categories
Index(['b', 'c', 'd'], dtype='object')

对于 pandas.Categorical

>>> cat = pd.Categorical(['a', 'b'], ordered=True)
>>> cat.categories
Index(['a', 'b'], dtype='object')

对于 pandas.CategoricalIndex

>>> ci = pd.CategoricalIndex(['a', 'c', 'b', 'a', 'c', 'b'])
>>> ci.categories
Index(['a', 'b', 'c'], dtype='object')
>>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a'])
>>> ci.categories
Index(['c', 'b', 'a'], dtype='object')