[SciPy-user] Sort by first n columns in an array

Neil Crighton neilcrighton@gmail....
Fri Jul 10 08:45:58 CDT 2009


Dharhas Pothina <Dharhas.Pothina <at> twdb.state.tx.us> writes:

> Yes, but I don't need just the first six columns sorted, I need the *entire* d
ataset (ie all columns) sorted
> by date.

I see. You could use argsort instead of sort to get the indices that sort the
array.

data = np.genfromtxt(filename, dtype=None, usecols=(0,1,2,3,4,5),
                     names='year,month,day,hour,min,sec')
ind = data.argsort(order='year month day hour min sec'.split())
alldata = np.genfromtxt(filename, dtype=None)
sorted_alldata = alldata[ind]

But it's probably better to let genfromtxt choose its own field names and then
sort the whole thing using the first 6 fields.

alldata = np.genfromtxt(filename, dtype=None)
alldata.sort(order='f0 f1 f2 f3 f4 f5'.split())


Neil




More information about the SciPy-user mailing list