0

I have the following numpy array (as an example):

my_array = [[3, 7, 0]
            [20, 4, 0]
            [7, 54, 0]]

I want to replace the 0's in the 3rd column of each row with a value of 5 only if the first index is odd. So the expected outcome would be:

my_array = [[3, 7, 5]
            [20, 4, 0]
            [7, 54, 5]]

I tried numpy.where and numpy.place, but couldn't get the expected results.

Is there an elegant way to do this with numpy functions?

1
  • np.place(my_array[:,2], my_array[:,0] % 2, 5). Please provide valid python examples with the expected type. Commented Jun 4, 2022 at 22:16

1 Answer 1

2

you can do this by indexing as:

my_array[my_array[:, 0] % 2 != 0, 2] = 5

# my_array[:, 0] % 2 != 0   --- Boolean shows modifying rows --> [ True False  True]
Sign up to request clarification or add additional context in comments.

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.