简体   繁体   中英

sort numpy 2d array by indice of column

I am using numpy in python. I have a 1D(nx1) array and a 2D(nxm) array. I used argsort to get a indice of the 1D array. Now I want to use that indice to sort my 2D(nxm) array's colum.

I want to know how to do it?

For example:

>>>array1d = np.array([1, 3, 0])
>>>array2d = np.array([[1,2,3],[4,5,6]])

>>>array1d_indice = np.argsort(array1d)
    array([2, 0, 1], dtype=int64)

I want use array1d_indice to sord array2d colum to get:
    [[3, 1, 2],
     [6, 4, 5]]

Or anyway easier to achieve this is welcome

If what you mean is that you want the columns sorted based on the vector, then you use argsort on the vector:

vi = np.argsort(vector)

then to arrange the columns of array in the right order,

sorted = array[:, tuple(vi)]

to get rows, switch around the order of : and tuple(vi)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM