Python Practice › NumPy Programs › Program 49

Replace numbers at or above a value

EasyNumPy Programs · 49 of 100

Write a NumPy program to replace numbers greater than or equal to 8 with 0.

Advertisement
Input
[[6, 5], [10, 9], [7, 8]]
Output
[[6 5]
 [0 0]
 [7 0]]

Approach

  1. np.where(condition, new, old).

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment