I have two sets of array data
and result
. result
contains the same elements in data
but with an extra column and in unsorted order. I want to rearrange the result
array so that it is in the same order as the rows in data
, while bringing the associated value into the last column with the rest of the row when doing the sorting.
data = np.array([[0,1,0,0],[1,0,0,0],[0,1,1,0],[0,1,0,1]])
result = np.array([[0,1,1,0,1],[1,0,0,0,0],[0,1,0,0,1],[0,1,0,1,0]])
# this is what the final sorted array should look like:
'''
array([[0, 1, 0, 0, 1],
[1, 0, 0, 0, 0],
[0, 1, 1, 0, 1],
[0, 1, 0, 1, 0]])
'''
I've tried doing argsort
in order to reverse data
into the sorted order then applying that to result
but argsort
seems to sort the order of the array based on each element, whereas I want the sort to treat each row of the data[:,4]
as a whole.
ind = np.argsort(data)
indind =np.argsort(ind)
ind
array([[0, 2, 3, 1],
[1, 2, 3, 0],
[0, 3, 1, 2],
[0, 2, 1, 3]])
What is a good way to do this kind of sorting by rows?