Reputation: 815
With two arrays:
X = np.array([[1,2,3], [2,3,1]])
X
array([[1, 2, 3],
[2, 3, 1]])
Y = np.array([['A','B', 'C'], ['A','B', 'C']])
Y
array([['A', 'B', 'C'],
['A', 'B', 'C']],
dtype='|S1')
I am trying to sort Y based on the values of X row by row without looping through each row, i.e
xord = X.argsort()
for i in range(X.shape[0]):
print Y[i][xord[i]]
['A' 'B' 'C']
['C' 'A' 'B']
Is there a more efficient way to sort array Y based on the corresponding row order of X?
Upvotes: 1
Views: 1179
Reputation: 36
Altering Mazdak's answer based on AdrianBoeh's comment, after I found this similar question here How to using numpy.argsort on a 2D array to sort another 2D array :
X = np.array([[1,2,3], [2,3,1]])
Y = np.array([['A','B', 'C'],['X','Y','Z']])
s=np.argsort(X)
np.take_along_axis(Y,s,axis=1)
It seems that the np.take(Y,s)
applies the indexes s
only to the first row of Y
, so np.take_along_axis
is necessary in this kind of problem if the rows of Y
are not all the same.
Upvotes: 1
Reputation: 107287
First you can use np.argsort
to get the indices of X
elements based on those position after sorting,then you can get the elements from Y
based on the indices of X
with np.take()
:
>>> s=np.argsort(X)
>>> np.take(Y,s)
array([['A', 'B', 'C'],
['C', 'A', 'B']],
dtype='|S1')
Upvotes: 1