8

I've got this array, named v, of dtype('float64'):

array([[  9.33350000e+05,   8.75886500e+06,   3.45765000e+02],
       [  4.33350000e+05,   8.75886500e+06,   6.19200000e+00],
       [  1.33360000e+05,   8.75886500e+06,   6.76650000e+02]])

... which I've acquired from a file by using the np.loadtxt command. I would like to sort it after the values of the first column, without mixing up the structure that keeps the numbers listed on the same line together. Using v.sort(axis=0) gives me:

array([[  1.33360000e+05,   8.75886500e+06,   6.19200000e+00],
       [  4.33350000e+05,   8.75886500e+06,   3.45765000e+02],
       [  9.33350000e+05,   8.75886500e+06,   6.76650000e+02]])

... i.e. places the smallest number of the third column in the first line, etc. I would rather want something like this...

array([[  1.33360000e+05,   8.75886500e+06,   6.76650000e+02],
       [  4.33350000e+05,   8.75886500e+06,   6.19200000e+00],
       [  9.33350000e+05,   8.75886500e+06,   3.45765000e+02]])

... where the elements of each line hasn't been moved relatively to each other.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
awoxho
  • 81
  • 1
  • 1
  • 2

4 Answers4

13

Try

v[v[:,0].argsort()]

(with v being the array). v[:,0] is the first column, and .argsort() returns the indices that would sort the first column. You then apply this ordering to the whole array using advanced indexing. Note that you get a sorte copy of the array.

The only way I know of to sort the array in place is to use a record dtype:

v.dtype = [("x", float), ("y", float), ("z", float)]
v.shape = v.size
v.sort(order="x")
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
5

Alternatively

Try

import numpy as np

order = v[:, 0].argsort()
sorted = np.take(v, order, 0)

'order' has the order of the first row. and then 'np.take' take the columns their corresponding order.

See the help of 'np.take' as

help(np.take)

take(a, indices, axis=None, out=None, mode='raise') Take elements from an array along an axis.

This function does the same thing as "fancy" indexing (indexing arrays
using arrays); however, it can be easier to use if you need elements
along a given axis.

Parameters
----------
a : array_like
    The source array.
indices : array_like
    The indices of the values to extract.
axis : int, optional
    The axis over which to select values. By default, the flattened
    input array is used.
out : ndarray, optional
    If provided, the result will be placed in this array. It should
    be of the appropriate shape and dtype.
mode : {'raise', 'wrap', 'clip'}, optional
    Specifies how out-of-bounds indices will behave.

    * 'raise' -- raise an error (default)
    * 'wrap' -- wrap around
    * 'clip' -- clip to the range

    'clip' mode means that all indices that are too large are

replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

Returns
-------
subarray : ndarray
    The returned array has the same type as `a`.

See Also
--------
ndarray.take : equivalent method

Examples
--------
>>> a = [4, 3, 5, 7, 6, 8]
>>> indices = [0, 1, 4]
>>> np.take(a, indices)
array([4, 3, 6])

In this example if `a` is an ndarray, "fancy" indexing can be used.

>>> a = np.array(a)
>>> a[indices]
array([4, 3, 6])
emesday
  • 6,078
  • 3
  • 29
  • 46
0

If you have instances where v[:,0] has some identical values and you want to secondarily sort on columns 1, 2, etc.., then you'll want to use numpy.lexsort() or numpy.sort(v, order=('col1', 'col2', etc..) but for the order= case, v will need to be a structured array.

Paul
  • 42,322
  • 15
  • 106
  • 123
0

An example application of numpy.lexsort() to sort the rows of an array and deals with ties in the first column. Note that lexsort effectively sorts columns and starts with the last column, so you need to reverse the rows of a then take the transpose before the lexsort, and finally transpose the result (you'd have thought this should be easier, but hey!):

In [1]: import numpy as np

In [2]: a = np.array([[1,2,3,4],[1,0,4,1],[0,4,1,1]])

In [3]: a[np.lexsort(np.flip(a, axis=1).T).T]
Out[3]: 
array([[0, 4, 1, 1],
       [1, 0, 4, 1],
       [1, 2, 3, 4]])

In [4]: a
Out[4]: 
array([[1, 2, 3, 4],
       [1, 0, 4, 1],
       [0, 4, 1, 1]])

Thanks go to @Paul for the suggestion to use lexsort.

drevicko
  • 14,382
  • 15
  • 75
  • 97