pandas.Series.cat.categories #

系列.cat。类别[来源] #

此分类的类别。

设置为每个类别分配新值(实际上是每个单独类别的重命名)。

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

加薪
值错误

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

也可以看看

rename_categories

重命名类别。

reorder_categories

重新排序类别。

add_categories

添加新类别。

remove_categories

删除指定的类别。

remove_unused_categories

删除不使用的类别。

set_categories

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

例子

为了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')