2

I have df column consisting of +ve and -ve columns.

       A          B
0      a           5
1      b         -13
2      c          15
3      d         -10

And is there a way to sort out +ve values ascending and -ve values descending

       A          B
0      a           5
1      c          15
2      d         -10
3      b         -13
bharatk
  • 4,202
  • 5
  • 16
  • 30

1 Answers1

5

First filter both with boolean indexing, sorting by DataFrame.sort_values and last concat together:

mask = df['B'].gt(0)
df = pd.concat([df[mask].sort_values('B'),
                df[~mask].sort_values('B', ascending=False)], ignore_index=True)
print (df)
   A   B
0  a   5
1  c  15
2  d -10
3  b -13
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252