1

I have a numpy 2D array with a range of 800. Now I only want to use the elements from index range 600 to 700 and set all other values to np.nan. The Index of the lasting elements should be the same as before. The length of the array should be the same too. Thanks!

1

1 Answer 1

1
import numpy as np
a = np.random.random ([800,4])

a[:600, :], a[700:, :] = np.nan, np.nan
Sign up to request clarification or add additional context in comments.

2 Comments

@denali11: Please be sure to mark it as the accepted answer in that case. You can do that by clicking the check mark next to the answer.
This is an excellent answer, but I find combining tuple unpacking with broadcasting a little confusing. Just to make the code a little more readable, I would use the equivalent two statements a[:600, :] = np.nan followed by a[700:, :] = np.nan instead.

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.