I would like to return a dataFrame with each row sorted (let’s say descending). So if I have the pandas.DataFrame named data:
In [38]: data
Out[38]:
c1 c2 c3 c4 c5 c6
Date
2012-10-22 0.973371 0.226342 0.968282 0.872330 0.273880 0.746156
2012-10-19 0.497048 0.351332 0.310025 0.726669 0.344202 0.878755
2012-10-18 0.315764 0.178584 0.838223 0.749962 0.850462 0.400253
2012-10-17 0.162879 0.068409 0.704094 0.712860 0.537545 0.009789
I would like the following returned:
In [39]: sorted_frame
Out[39]:
0 1 2 3 4 5
Date
2012-10-22 0.973371 0.968282 0.872332 0.746156 0.273880 0.226342
2012-10-19 0.878755 0.726669 0.497048 0.351332 0.344202 0.310025
2012-10-18 0.850462 0.838223 0.749962 0.400253 0.315764 0.178584
2012-10-17 0.712860 0.704094 0.537545 0.162879 0.068409 0.009789
I’ve tried DataFrame.sort(axis = 1) however, that doesn’t achieve the desired result:
In [40]: data.sort(axis = 1)
Out[43]:
c1 c2 c3 c4 c5 c6
Date
2012-10-22 0.973371 0.226342 0.968282 0.872330 0.273880 0.746156
2012-10-19 0.497048 0.351332 0.310025 0.726669 0.344202 0.878755
2012-10-18 0.315764 0.178584 0.838223 0.749962 0.850462 0.400253
2012-10-17 0.162879 0.068409 0.704094 0.712860 0.537545 0.009789
I’ve created the following function that accomplishes what I’m looking for (using the pandas.TimeSeries.order()):
import numpy
def sorted_by_row(frame, ascending = False):
vals = numpy.tile(numpy.nan,frame.shape)
for row in numpy.arange(frame.shape[0]):
vals[row, :] = frame.ix[row, :].order(ascending = ascending)
return pandas.DataFrame(vals, index = frame.index)
However, my goal is to be able to use a row-wise function in the DataFrame.apply() method (so I can apply the desired functionality to other functions I build). I’ve tried:
#TimeSeries.order() sorts a pandas.TimeSeries object
data.apply(lambda x: x.order(), axis = 1)
But again, I’m not getting the desired DataFrame above (I’ve outputted enough DataFrame's so I’ll spare the page the real estate).
Your help is greatly appreciated,
-B
Well, it’s not too easy to do with pandas out of the box. First, familiarize yourself with
argsort:These are the indirect sort indices for each row. Now you want to do something like:
Not that satisfying but it gets the job done:
More generally you can do:
But you’re responsible for assigning new columns yourself