import numpy as np
array = np.random.uniform(0.0, 1.0, (100, 2))
array[(array[:, 1:2] < 3.0).flatten()][:][1:2] = 4.0
I want to change the second value of the rows who is less than 3.0 to 4.0, but the above code does not work. I tried to search a little bit, it appears that fancy slicing always just operates on a copy of the original array. Is that true? How to do the correct assignment in this case?
CodePudding user response:
Just use multi index (boolean index for the 1st dimension & integer index for 2nd dimension) to mutate in place:
array[array[:, 1] < 3, 1] = 4
array
# array([[2.59733369e-01, 4.00000000e 00],
# [5.08406931e-01, 4.00000000e 00],
# ...