pandas.Series.cat.reorder_categories #

系列.cat。reorder_categories ( * args , ** kwargs ) [来源] #

按照 new_categories 中指定的方式对类别进行重新排序。

new_categories需要包含所有旧类别并且没有新类别项目。

参数
new_categories类索引

类别按新顺序排列。

有序布尔值,可选

是否将分类视为有序分类。如果未给出,请勿更改订购信息。

返回
分类的

类别重新排序的分类。

加薪
值错误

如果新类别不包含所有旧类别项目或任何新类别项目

也可以看看

rename_categories

重命名类别。

add_categories

添加新类别。

remove_categories

删除指定的类别。

remove_unused_categories

删除不使用的类别。

set_categories

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

例子

为了pandas.Series

>>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
>>> ser = ser.cat.reorder_categories(['c', 'b', 'a'], ordered=True)
>>> ser
0   a
1   b
2   c
3   a
dtype: category
Categories (3, object): ['c' < 'b' < 'a']
>>> ser.sort_values()
2   c
1   b
0   a
3   a
dtype: category
Categories (3, object): ['c' < 'b' < 'a']

为了pandas.CategoricalIndex

>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'])
>>> ci
CategoricalIndex(['a', 'b', 'c', 'a'], categories=['a', 'b', 'c'],
                 ordered=False, dtype='category')
>>> ci.reorder_categories(['c', 'b', 'a'], ordered=True)
CategoricalIndex(['a', 'b', 'c', 'a'], categories=['c', 'b', 'a'],
                 ordered=True, dtype='category')