Sort DataFrame on column of intervals

Viewed 2106

My output looks like this:

     binnedb   Proba-A   Proba-B Esperance-A Esperance-B
0    (0.0101, 0.0202]  0.547826  0.539130    0.007817    0.007693
1    (0.0302, 0.0402]  0.547826  0.539130    0.005963    0.005854
2    (0.0201, 0.0302]  0.547826  0.539130    0.008360    0.008227

What I would like to do is to sort the df in an ascending order based on the binnedb column(which will be also sorted in ascending order). Please let me know if you don't understand the question. That is what I tried so far: df.sort_values(by=['binnedb'], ascending = False)

But it does not work... thanks!

2 Answers

Since it is inverval type column , you can using left to get the left range and sort base on it .

df['sortkey']=df.binnedb.map(lambda x : x.left)
df=df.sort_values('sortkey')

Interval columns are actually categorical columns which follow a specific ordering. If "binnedb" is categorical column, you can access its category codes and use argsort:

df = df.iloc[df['binnedb'].cat.codes.argsort()]
Related