75

I'm looking for a one line solution that would help me do the following.

Suppose I have

array = np.array([10, 20, 30, 40, 50])

I'd like to rearrange it based upon an input ordering. If there were a numpy function called arrange, it would do the following:

newarray = np.arrange(array, [1, 0, 3, 4, 2])
print newarray

    [20, 10, 40, 50, 30]

Formally, if the array to be reordered is m x n, and the "index" array is 1 x n, the ordering would be determined by the array called "index".

Does numpy have a function like this?

5 Answers 5

98

You can simply use your "index" list directly, as, well, an index array:

>>> arr = np.array([10, 20, 30, 40, 50])
>>> idx = [1, 0, 3, 4, 2]
>>> arr[idx]
array([20, 10, 40, 50, 30])

It tends to be much faster if idx is already an ndarray and not a list, even though it'll work either way:

>>> %timeit arr[idx]
100000 loops, best of 3: 2.11 µs per loop
>>> ai = np.array(idx)
>>> %timeit arr[ai]
1000000 loops, best of 3: 296 ns per loop
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! What if the "index" is 2d array? For example I want to turn [[1,2], ["a", "b"] into [[2,1], ["a", "b"]] by using the index [[1,0], [0,1]]. I know an ugly way to do is using for-loop to re-arrange each row of the array using the corresponding row of index array. But there gotta be a faster way.
Note: the indexing array idx can not be a tuple. This was somehow counter-intuitive, but your solution works perfectly. Thanks
I know this is old but I am looking for the same solution. The line arr[ai] seems to have no effect at all. python version 3.7.3
Check Dong Dong Justin's answer for those who it isn't working. I had to convert It using np.argsort()
@Skywalker326 I know it's old, but have you figured that out yet lol? I can't get the 2d works just like that
9

for those whose index is 2d array, you can use map function. Here is an example:

a = np.random.randn(3, 3)
print(a)
print(np.argsort(a))

print(np.array(list(map(lambda x, y: y[x], np.argsort(a), a))))

the output is

[[-1.42167035  0.62520498  2.02054623]
 [-0.17966393 -0.01561566  0.24480554]
 [ 1.10568543  0.00298402 -0.71397599]]
[[0 1 2]
 [0 1 2]
 [2 1 0]]
[[-1.42167035  0.62520498  2.02054623]
 [-0.17966393 -0.01561566  0.24480554]
 [-0.71397599  0.00298402  1.10568543]]

Comments

9

For those who have the same confusion, I am actually looking for a slightly different version of "rearrange array based upon index". In my situation, the index array is indexing the target array instead of the source array. In other words, I am try to rearrange an array based on its position in the new array.

In this case, simply apply an argsort before indexing. E.g.

>>> arr = np.array([10, 20, 30, 40, 50])
>>> idx = [1, 0, 3, 4, 2]
>>> arr[np.argsort(idx)]
array([20, 10, 50, 30, 40])

Note the difference between this result and the desired result by op.

One can verify back and forth

>>> arr[np.argsort(idx)][idx] == arr
array([ True,  True,  True,  True,  True])
>>> arr[idx][np.argsort(idx)] == arr
array([ True,  True,  True,  True,  True])

Comments

1

If you want to sort it but descending:

a = np.array([1,2,3,4,5])
np.argsort(a)
> array([0, 1, 2, 3, 4])
np.argsort(-a)
> array([4, 3, 2, 1, 0])

Comments

1

Numpy has a feature called Integer Array Indexing that makes this easy:

array = np.array([10,20,30,40,50])
print(array)
ind = np.array([1,0,3,4,2])
print(ind)
newarray = array[ind]
print(newarray)
[10 20 30 40 50]
[1 0 3 4 2]
[20 10 40 50 30]

Use an index array as the starting element.

Also works for multi-dimensional arrays:

array = np.array([[10,20,30,40,50],[60,70,80,90,100]])
print(array)
ind = np.array([1,0,3,4,2])
print(ind)
newarray = array[:, ind]
print(newarray)
[[ 10  20  30  40  50]
 [ 60  70  80  90 100]]
[1 0 3 4 2]
[[ 20  10  40  50  30]
 [ 70  60  90 100  80]]

This reorders by column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.