If I have enum class:
from enum import Enum
class Colors(Enum):
RED = 1
ORANGE = 2
GREEN = 3
And if I have a dataframe whose one column is color (it can be in lowercase to):
>>> import pandas as pd
>>> df = pd.DataFrame({'X':['A', 'B', 'C', 'A'], 'color' : ['GREEN', 'RED', 'ORANGE', 'ORANGE']})
>>> df
X color
0 A GREEN
1 B RED
2 C ORANGE
3 A ORANGE
How to make color column as categorical type respecting Color class values, and sort the dataframe by "color" and "X" (ascending)?
For example, the dataframe above should be sorted as:
X, color
--------
B, RED
A, ORANGE
C, ORANGE
A, GREEN
Combination of this answer and this one: use a pd.Categorical
to sort by the Colors
class (with a slight edit to change its str
):
from enum import Enum
import pandas as pd
df = pd.DataFrame({'X':['A', 'B', 'C', 'A'], 'color' : ['GREEN', 'RED', 'ORANGE', 'ORANGE']})
class Colors(Enum):
RED = 1
ORANGE = 2
GREEN = 3
def __str__(self):
return self.name
df['color'] = pd.Categorical(df['color'], [str(i) for i in Colors], ordered=True)
df = df.sort_values(['color','X'])
Result:
X color
1 B RED
3 A ORANGE
2 C ORANGE
0 A GREEN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With