0

My dataset df look like this:

date           high
2018-01-01     -1
2018-01-02     1
2018-01-03     -2
2018-01-04     0
...., ....
2018-12-31     1

Where,

-2 >= high <= 2

high is always between -2 and 2

I want to sort the value of high in the following pattern:

To start, Group all 0 and sort by date and so on for other values.

Sort the high value in the following order:

0
1
-1
2
-2

It would be best if it's flexible enough that I can change the order if required.

I know how to sort in asc or desc by doing this:

df.sort_values(by='high', ascending=False)

Could you please help me solve how do I sort using predetermined values?

floss
  • 2,603
  • 2
  • 20
  • 37

1 Answers1

5

You need to define high as a Categorical Series with order as your choice.

order = [0 , 1, -1, 2, -2]
df['high'] = pd.Categorical(df['high'], order)
df.sort_values(['high', 'date'])

Output:

        date    high
3   2018-01-04  0
1   2018-01-02  1
4   2018-12-31  1
0   2018-01-01  -1
2   2018-01-03  -2
harvpan
  • 8,571
  • 2
  • 18
  • 36