3

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!

cs95
  • 379,657
  • 97
  • 704
  • 746
Viktor.w
  • 1,787
  • 2
  • 20
  • 46

2 Answers2

4

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')
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thanks for the answer, but the first line of code does not seem to take the left value of the interval... any idea why? – Viktor.w Dec 31 '18 at 15:42
3

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()]
cs95
  • 379,657
  • 97
  • 704
  • 746
  • @W-B and to you as well :) – cs95 Dec 31 '18 at 15:32
  • 1
    @AmiTavory No problem. The original link was for https://stackoverflow.com/questions/53927460/how-do-i-slice-or-filter-mutliindex-dataframe-levels/53927461#53927461 But actually, I have a list of all the pandas canonicals I have written (all of them written last month) in my About Me, under the heading "(Actively Maintained) Pandas Canonicals". Please do go through them when you have the time. Thanks again :) – cs95 Jan 02 '19 at 06:54