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 Answer
import numpy as np
a = np.random.random ([800,4])
a[:600, :], a[700:, :] = np.nan, np.nan
2 Comments
Jeremy Caney
@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.
joanis
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.