pandas.Categorical.reorder_categories — pandas 3.0.0.dev0+2102.g839747c3f6 documentation (original) (raw)
Categorical.reorder_categories(new_categories, ordered=None)[source]#
Reorder categories as specified in new_categories.
new_categories
need to include all old categories and no new category items.
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 any new ones
Examples
For 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']
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')