Open In App

Replace NumPy Array Elements that doesn't Satisfy the Given Condition

Last Updated : 09 Oct, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

If elements in a NumPy array don’t meet a certain condition, you replace them with another value (like 0, -1, or NaN), while keeping the elements that satisfy the condition unchanged. For example: Suppose the condition is “keep only numbers greater than 3”. A=[1,  4,  2,  6,  3], after replacing elements not satisfying the condition: A′=[0,  4,  0,  6,  0].

For this, we can use Relational operators like '>', '<', etc and other functions like numpy.where().

Using Relational operators

Relational operators can directly compare array elements with a constant. If the condition is True, the element is replaced. Otherwise, it remains unchanged.

Example 1: In this example, we replace all values greater than 50 with 15.50 in a 1-D array.

Python
import numpy as np

n_arr = np.array([75.42436315, 42.48558583, 60.32924763])
print("Array:")
print(n_arr)

n_arr[n_arr > 50.] = 15.50 
print("Result:")
print(n_arr)

Output
Array:
[75.42436315 42.48558583 60.32924763]
Result:
[15.5        42.48558583 15.5       ]

Explanation: n_arr[n_arr > 50.] = 15.50 build a boolean mask n_arr > 50. (True for elements > 50) and replace those positions with 15.50. This modifies the array in place.

Note: dtype remains float since both original and replacement values are floats.

Example 2: Here, we replace all elements greater than 30 with 5.25 in a 2-D array.

Python
import numpy as np

n_arr = np.array([[45.42436315, 52.48558583, 10.32924763],[5.7439979, 50.58220701, 25.38213418]])
print("Array:")
print(n_arr)

n_arr[n_arr > 30.] = 5.25 
print("Result:")
print(n_arr)

Output
Array:
[[45.42436315 52.48558583 10.32924763]
 [ 5.7439979  50.58220701 25.38213418]]
Result:
[[ 5.25        5.25       10.32924763]
 [ 5.7439979   5.25       25.38213418]]

Explanation:

  • n_arr > 30 produces a boolean matrix of same shape.
  • n_arr[n_arr > 30.] = 5.25 selects all True positions and replaces them with 5.25 in-place.

Note: This works for arrays of any dimensionality the boolean mask matches the array shape.

Example 3: This program replaces all elements less than 10 with NaN in a 3-D array.

Python
import numpy as np

n_arr = np.array([[[11, 25.5, 70.6], [30.9, 45.5, 55.9], [20.7, 45.8, 7.1]],
                  [[50.1, 65.9, 8.2], [70.4, 85.8, 10.3], [11.3, 22.2, 33.6]],
                  [[19.9, 69.7, 36.8], [1.2, 5.1, 24.4], [4.9, 20.8, 96.7]]])
print("Array:")
print(n_arr)

n_arr[n_arr < 10.] = np.nan
print("Result:")
print(n_arr)

Output

Array:
[[[11. 25.5 70.6]
[30.9 45.5 55.9]
[20.7 45.8 7.1]]

[[50.1 65.9 8.2]
[70.4 85.8 10.3]
[11.3 22.2 33.6]]

[[19.9 69.7 36.8]
[ 1.2 5.1 24.4]
[ 4.9 20.8 96.7]]]
Result:
[[[11. 25.5 70.6]
[30.9 45.5 55.9]
[20.7 45.8 nan]]

[[50.1 65.9 nan]
[70.4 85.8 10.3]
[11.3 22.2 33.6]]

[[19.9 69.7 36.8]
[ nan nan 24.4]
[ nan 20.8 96.7]]]

Explanation:

  • n_arr < 10 makes a boolean mask across all elements.
  • n_arr[n_arr < 10.] = np.nan assigns np.nan wherever the mask is True.
  • Array prints show nan in place of values < 10.

Note: assigning np.nan will upcast integer arrays to floats here the array is already float so nan fits naturally.

Using numpy.where()

The numpy.where() function allows conditional replacement. It checks each element and based on the condition, returns a new value.

Example 1: In this example, elements greater than or equal to 25 are replaced with 0, while others remain the same.

Python
import numpy as np

n_arr = np.array([[45, 52, 10], [1, 5, 25]])
print("Array:")
print(n_arr)

print("Result:")
print(np.where(n_arr >= 25, 0, n_arr))

Output
Array:
[[45 52 10]
 [ 1  5 25]]
Result:
[[ 0  0 10]
 [ 1  5  0]]

Explanation:

  • n_arr >= 25 is a boolean mask.
  • np.where(n_arr >= 25, 0, n_arr) returns a new array where positions satisfying the condition are replaced by 0, otherwise original n_arr values are used.
  • The result prints as an integer array because both replacements and kept values are integers.

Example 2: Here, we replace all values less than or equal to 25 with NaN and all others with 1.

Python
import numpy as np

n_arr = np.array([[45, 52, 10], [1, 5, 25], [50, 40, 81]])
print("Array:")
print(n_arr)

print("Result:")
print(np.where(n_arr <= 25, np.nan, 1))

Output
Array:
[[45 52 10]
 [ 1  5 25]
 [50 40 81]]
Result:
[[ 1.  1. nan]
 [nan nan nan]
 [ 1.  1.  1.]]

Explanation: 

  • n_arr <= 25 creates a boolean mask identifying elements to replace with np.nan.
  • np.where(..., np.nan, 1) constructs a new array with np.nan where condition is True, and 1 otherwise.
  • Because np.nan is a float, the returned array is float dtype and prints nan where indicated.

Example 3: In this example, negative values are replaced with 0, non-negative values remain unchanged.

Python
import numpy as np

n_arr = np.array([-5, 10, -2, 7, 0])
print("Array:")
print(n_arr)

print("Result:")
print(np.where(n_arr < 0, 0, n_arr))

Output
Array:
[-5 10 -2  7  0]
Result:
[ 0 10  0  7  0]

Explanation:

  • n_arr < 0 forms a boolean mask for negatives.
  • np.where(n_arr < 0, 0, n_arr) returns a new array where negatives are replaced by 0, others kept as-is.
  • Result dtype remains integer.

Explore