This may sound silly, but I just can't seem to figure it out. I have a Pandas dataframe like this:
N1 N2 N3 N4 N5
0 48 20 45 21 12
1 32 16 29 41 36
2 41 42 34 13 9
3 39 37 4 7 33
4 32 3 1 39 21
... ... ... ... ... ...
1313 1 5 27 36 42
1314 18 20 35 38 48
1315 12 34 37 38 42
1316 18 23 37 41 42
1317 2 10 18 34 35
and I want to sort each row so that the row is re-arranged from min to max. I don't want the column labels to change. ie it looks like this:
N1 N2 N3 N4 N5
0 48 45 21 20 12
1 41 32 36 29 16
2 42 41 34 13 9
I've tried a for loop with iloc, running through the index,one row at a time, applying sort_values, but it doesn't work. Any help?
You can sorting rows by
numpy.sort
, swap ordering for descending order by[:, ::-1]
and pass to DataFrame constructor if performance is important:A bit worse performance if assign back:
Performance: