pyspark.pandas.Series.cat.reorder_categories

cat. reorder_categories ( new_categories : Union [ pandas.core.indexes.base.Index , List ] , ordered : Optional [ bool ] = None ) → Optional [ ps.Series ]

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

new_categories 需要包含所有旧类别,且不包含任何新类别项。

Parameters
new_categories Index-like

新顺序中的类别。

ordered bool, optional

分类是否被视为有序分类。如果没有给出,则不改变有序信息。

Returns
cat Series or None

移除类别后的分类数据,如果 inplace=True 则为None。

Raises
ValueError

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

另请参阅

rename_categories

重命名类别。

add_categories

添加新类别。

remove_categories

移除指定的分类。

remove_unused_categories

移除未使用的分类。

set_categories

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

示例

>>> s = ps.Series(list("abbccc"), dtype="category")
>>> s  
0    a
1    b
2    b
3    c
4    c
5    c
dtype: category
Categories (3, object): ['a', 'b', 'c']
>>> s.cat.reorder_categories(['c', 'b', 'a'], ordered=True)  
0    a
1    b
2    b
3    c
4    c
5    c
dtype: category
Categories (3, object): ['c' < 'b' < 'a']