python - How to sort a numpy array based on the values in a specific row? -
python - How to sort a numpy array based on the values in a specific row? -
i wondering how able sort whole array values in 1 of columns.
i have :
array([5,2,8,2,4])
and:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])
i want append first array sec 1 this:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [5, 2, 8, 2, 4]])
and sort array appended row either this:
array([[1, 3, 4, 0, 2], [6, 8, 9, 5, 7], [11, 13, 14, 10, 12], [16, 18, 19, 15, 17], [21, 23, 24, 20, 22], [2, 2, 4, 5, 8]])
or this:
array([[ 2, 1, 3, 4, 0], [ 7, 6, 8, 9, 5], [12, 11, 13, 14, 10], [17, 16, 18, 19, 15], [22, 21, 23, 24, 20], [ 8, 5, 4, 2, 2]])
and remove appended column get:
array([[1, 3, 4, 0, 2], [6, 8, 9, 5, 7], [11, 13, 14, 10, 12], [16, 18, 19, 15, 17], [21, 23, 24, 20, 22]])
or:
array([[ 2, 1, 3, 4, 0], [ 7, 6, 8, 9, 5], [12, 11, 13, 14, 10], [17, 16, 18, 19, 15], [22, 21, 23, 24, 20]])
is there code carry out procedure. new python. lot!
you can utilize numpy.argsort list sorted indices of array. using can rearrange columns of matrix.
import numpy np c = np.array([5,2,8,2,4]) = np.array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) = np.argsort(c) = a[:,i]
python arrays sorting numpy
Comments
Post a Comment