Sorting arrays in NumPy by column

后端 未结 13 2220
既然无缘
既然无缘 2020-11-22 03:47

How can I sort an array in NumPy by the nth column?

For example,

a = array([[9, 2, 3],
           [4, 5, 6],
           [7, 0, 5]])

相关标签:
13条回答
  • 2020-11-22 04:27

    #for sorting along column 1

    indexofsort=np.argsort(dataset[:,0],axis=-1,kind='stable') 
    dataset   = dataset[indexofsort,:]
    
    0 讨论(0)
  • 2020-11-22 04:30

    Here is another solution considering all columns (more compact way of J.J's answer);

    ar=np.array([[0, 0, 0, 1],
                 [1, 0, 1, 0],
                 [0, 1, 0, 0],
                 [1, 0, 0, 1],
                 [0, 0, 1, 0],
                 [1, 1, 0, 0]])
    

    Sort with lexsort,

    ar[np.lexsort(([ar[:, i] for i in range(ar.shape[1]-1, -1, -1)]))]
    

    Output:

    array([[0, 0, 0, 1],
           [0, 0, 1, 0],
           [0, 1, 0, 0],
           [1, 0, 0, 1],
           [1, 0, 1, 0],
           [1, 1, 0, 0]])
    
    0 讨论(0)
  • 2020-11-22 04:31

    A little more complicated lexsort example - descending on the 1st column, secondarily ascending on the 2nd. The tricks with lexsort are that it sorts on rows (hence the .T), and gives priority to the last.

    In [120]: b=np.array([[1,2,1],[3,1,2],[1,1,3],[2,3,4],[3,2,5],[2,1,6]])
    In [121]: b
    Out[121]: 
    array([[1, 2, 1],
           [3, 1, 2],
           [1, 1, 3],
           [2, 3, 4],
           [3, 2, 5],
           [2, 1, 6]])
    In [122]: b[np.lexsort(([1,-1]*b[:,[1,0]]).T)]
    Out[122]: 
    array([[3, 1, 2],
           [3, 2, 5],
           [2, 1, 6],
           [2, 3, 4],
           [1, 1, 3],
           [1, 2, 1]])
    
    0 讨论(0)
  • 2020-11-22 04:34

    Simply using sort, use coloumn number based on which you want to sort.

    a = np.array([1,1], [1,-1], [-1,1], [-1,-1]])
    print (a)
    a=a.tolist() 
    a = np.array(sorted(a, key=lambda a_entry: a_entry[0]))
    print (a)
    
    0 讨论(0)
  • 2020-11-22 04:35

    @steve's answer is actually the most elegant way of doing it.

    For the "correct" way see the order keyword argument of numpy.ndarray.sort

    However, you'll need to view your array as an array with fields (a structured array).

    The "correct" way is quite ugly if you didn't initially define your array with fields...

    As a quick example, to sort it and return a copy:

    In [1]: import numpy as np
    
    In [2]: a = np.array([[1,2,3],[4,5,6],[0,0,1]])
    
    In [3]: np.sort(a.view('i8,i8,i8'), order=['f1'], axis=0).view(np.int)
    Out[3]: 
    array([[0, 0, 1],
           [1, 2, 3],
           [4, 5, 6]])
    

    To sort it in-place:

    In [6]: a.view('i8,i8,i8').sort(order=['f1'], axis=0) #<-- returns None
    
    In [7]: a
    Out[7]: 
    array([[0, 0, 1],
           [1, 2, 3],
           [4, 5, 6]])
    

    @Steve's really is the most elegant way to do it, as far as I know...

    The only advantage to this method is that the "order" argument is a list of the fields to order the search by. For example, you can sort by the second column, then the third column, then the first column by supplying order=['f1','f2','f0'].

    0 讨论(0)
  • 2020-11-22 04:36

    From the Python documentation wiki, I think you can do:

    a = ([[1, 2, 3], [4, 5, 6], [0, 0, 1]]); 
    a = sorted(a, key=lambda a_entry: a_entry[1]) 
    print a
    

    The output is:

    [[[0, 0, 1], [1, 2, 3], [4, 5, 6]]]
    
    0 讨论(0)
提交回复
热议问题