python indexing and affect by ndarray line


Something is wrong in my script and I found the error, but I'm completely stuck.

there is array b which contains two elements:

b = np.zeros ((1,2))

b[0,0] = 272
b[0,1] = 1578

I want to check if there are elements in the second columns, greater than a value, and if so, assign that value a zero.

the command

b[ b[:,1] >= 1000 ] = 0

changes both elements to 0 instead of b[0,1]

what am I missing?

thanks in advance


If I understand you correctly, you only want to set the second column to zero (if its value is >1000)? I extended your example to have at least two rows, but just tested it to work also with one:

b = np.array([[123, 456],
              [789, 101112]])
mask = b[:,1] > 1000
b[mask,1] = 0

print b

I defined mask to explain it better - you can substitute it in. mask is then a boolean vector with one element for each row, in this case [False, True]. In the last step, that mask is used to mask out the selected rows and assign zero to the first column element.