[SciPy-user] Sort by first n columns in an array
Neil Crighton
neilcrighton at gmail.com
Wed Jul 8 19:14:36 EDT 2009
Dharhas Pothina <Dharhas.Pothina <at> twdb.state.tx.us> writes:
> I am reading in an array with np.genfromtxt and I need to sort it by the first
6 columns (ie year,month etc). I
> do not know the number of column before reading the array in.
>
> >From the documentation for sorting it looks like I can use the 'order'
keyword to do what I need if I have a
> structured array with names for each column. I haven't been able to work out
how to convert the array I have
> to an ordered array though. Also as far as I can tell I can't set the dtype
with field names in np.genfromtxt
> without previously knowing the number of columns in the data file.
>
> I don't want to use the timeseries toolkit for this since it is on a machine
that hasn't got that package installed.
>
> I know I must be missing something simple. Any help is appreciated.
>
> - dharhas
>
Something like this should work:
data = np.genfromtxt(filename, dtype=None, usecols=(0,1,2),
names='year,month,day')
data.sort(order=['year', 'month', 'day'])
Neil
More information about the SciPy-User
mailing list