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
  • 3,964
  • 5
  • 13
  • 28

1 Answers1

4

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
  • 729,927
  • 78
  • 1,141
  • 1,090