Python Practice › NumPy Programs › Program 49
Replace numbers at or above a value
Write a NumPy program to replace numbers greater than or equal to 8 with 0.
Input
[[6, 5], [10, 9], [7, 8]]
Output
[[6 5] [0 0] [7 0]]
Approach
np.where(condition, new, old).
Solution
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])print(np.where(a >= 8, 0, a))Output
[[6 5] [0 0] [7 0]]
Learn the concept
New to this topic? Read the lesson, then come back.