I have an ordered categorical variable in my dataframe like the following:
CategoricalDtype(categories=['UNDER $1 000', '$1 000 TO 2 999', '$3 000 TO 3 999',
'$90000 - $109999', '$110000 OR OVER', 'REFUSED'],
ordered=True)
For CategoricalIndex
in a dataframe I know I can do the following:
df.sort_index(ascending=False, inplace=True)
I tried the method I find here for the CategoricalDtype
object:
from pandas import Categorical
Categorical.sort(ascending=False)
but it doesn't work and returned:
AttributeError: type object 'Categorical' has no attribute 'sort'.
Thus, I wonder if there is an easy way to reverse the order for CategoricalDtype
with Pandas.
You can use list slicing / NumPy array syntax, i.e. [::-1]
, to reverse the order. This is natural because dtype.categories
returns a pd.Index
object, which has an underlying NumPy array.
from pandas.api.types import CategoricalDtype
dtype = CategoricalDtype(categories=['UNDER $1 000', '$1 000 TO 2 999', '$3 000 TO 3 999',
'$90000 - $109999', '$110000 OR OVER', 'REFUSED'],
ordered=True)
dtype_reversed = CategoricalDtype(categories=dtype.categories[::-1], ordered=True)
# CategoricalDtype(categories=['REFUSED', '$110000 OR OVER', '$90000 - $109999',
# '$3 000 TO 3 999', '$1 000 TO 2 999', 'UNDER $1 000'],
# ordered=True)