'reverse the order of CategoricalDtype in Pandas
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.
Solution 1:[1]
If you are only looking for a CategoricalIndex object with the reversed list of categories, you might consider creating a new object with the reversed list of categories from your original instance.
if x is your existing CategoricalDtype object:
x_reversed = CategoricalDtype(categories=reversed(x.categories), ordered=True)
Solution 2:[2]
I just discovered another way to do it.
Since CategoricalDtype in pandas has an attribute cat.categories, we can call it from a variable right away and reserve its order directly by using reversed() or [::-1]. If a pandas Series is categorical, pandas also offers lots of methods like cat.set_categories. Thus, for a variable named var in the dataframe, we can do the following:
order = df.var.cat.categories[::-1]
df.var.cat.set_categories(new_categories=order, ordered=True, inplace=True)
By using this method, we don't have to create a CategoricalDtype object or importing anything. It also saves us from using .astype() to replace the old order in the categorical variable as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
