1

I have an array of the shape (10296, 6). I want to swap the two last elements in the subarray.

a = [[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]...

So that 5 and 6 of each array is swapped into:

a = [[1, 2, 3, 4, 6, 5][1, 2, 3, 4, 6, 5]...

1 Answer 1

3

Try advanced slicing in numpy. Read more here -

import numpy as np

a = np.array([[1, 2, 3, 4, 5, 6],
              [1, 2, 3, 4, 5, 6]])

a[:,[4, 5]] = a[:,[5, 4]]
array([[1, 2, 3, 4, 6, 5],
       [1, 2, 3, 4, 6, 5]])
Sign up to request clarification or add additional context in comments.

2 Comments

oh lol, i knew i was off
hey, if it works, its fine :)

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.