If you want to replace elements of a NumPy array based on a condition, use np.where. The code above shows how it works.
np.where
Link to the source code.