- API reference
- Series
- pandas.Serie...
pandas.Series.cat.reorder_categories#
- Series.cat.reorder_categories(*args,**kwargs)[source]#
Reorder categories as specified in new_categories.
new_categoriesneed to include all old categories and no new categoryitems.- Parameters:
- new_categoriesIndex-like
The categories in new order.
- orderedbool, optional
Whether or not the categorical is treated as a ordered categorical.If not given, do not change the ordered information.
- Returns:
- Categorical
Categorical with reordered categories.
- Raises:
- ValueError
If the new categories do not contain all old category items or anynew ones
See also
rename_categoriesRename categories.
add_categoriesAdd new categories.
remove_categoriesRemove the specified categories.
remove_unused_categoriesRemove categories which are not used.
set_categoriesSet the categories to the specified ones.
Examples
For
pandas.Series:>>>ser=pd.Series(['a','b','c','a'],dtype='category')>>>ser=ser.cat.reorder_categories(['c','b','a'],ordered=True)>>>ser0 a1 b2 c3 adtype: categoryCategories (3, object): ['c' < 'b' < 'a']
>>>ser.sort_values()2 c1 b0 a3 adtype: categoryCategories (3, object): ['c' < 'b' < 'a']
>>>ci=pd.CategoricalIndex(['a','b','c','a'])>>>ciCategoricalIndex(['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')
On this page