HOWTO · NumPy
How to Replace Values in NumPy
There are three main methods that can be used to replace values inside a NumPy array in Python, the numpy.clip() function, the numpy.minimum() function, and the indexing method.
On this page
To replace values in a NumPy array, use numpy.clip() or combine numpy.minimum() and numpy.maximum() to clamp values between two limits. For a condition such as “replace every value above 5,” use boolean indexing and assignment. The first two approaches create a result array and leave the input unchanged; indexed assignment updates the original array.
NumPy Replace Values With the numpy.clip() Function
Use numpy.clip() when every value should stay within a lower and upper limit. Values below the lower limit become that limit, and values above the upper limit become the upper limit. With no out argument, the function returns a clipped result array and leaves the input array unchanged. Run this example:
import numpy as np
array = np.array([1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 9])
result = np.clip(array, 0, 5)
print(result)
Output:
[1 2 3 4 5 5 5 5 5 5 5 5]
Here, 0 is the lower limit and 5 is the upper limit. The values already between those limits remain the same, while larger values are capped at 5. Assigning the return value to result keeps the original array available for later calculations.
NumPy Replace Values With the numpy.minimum() and numpy.maximum() Functions
You can apply the same two limits with numpy.minimum() and numpy.maximum(). First, numpy.minimum() applies the upper cap by choosing the smaller of each array value and 5. Then numpy.maximum() applies the lower floor by choosing the larger of each intermediate value and 0. Both operations return arrays, so this sequence also keeps array unchanged.
import numpy as np
array = np.array([1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 9])
result1 = np.minimum(array, 5)
result2 = np.maximum(result1, 0)
print(result2)
Output:
[1 2 3 4 5 5 5 5 5 5 5 5]
The order matters when applying both limits: cap high values first, then raise values below the floor. Replace 5 and 0 with the upper and lower limits you need. If you only need one limit, use the corresponding function by itself: numpy.minimum() for an upper cap or numpy.maximum() for a lower floor.
NumPy Replace Values With the Array Indexing Method in Python
For a condition-based replacement, select matching elements with a Boolean mask and assign the replacement value through that selection. This is useful when the rule is not simply a pair of bounds, such as changing values above a threshold. The assignment operates on the original array, so make a copy first if you need to preserve it.
import numpy as np
array = np.array([1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 9])
array[array > 5] = 5
print(array)
Output:
[1 2 3 4 5 5 5 5 5 5 5 5]
The expression array > 5 creates a Boolean mask that is true for each value above 5. Indexing with that mask selects those elements, and assigning 5 replaces them in place. To replace values below a threshold, change the comparison to <; to target a range or another condition, combine Boolean comparisons with & or | and put parentheses around each comparison.