Python - Replace negative value with zero in numpy array


In this article we will see method to replace negative values with zero. If we talk about data analysis then handling negative values is very crucial step to ensure meaningful calculation. So, here you will learn about various methods using which we can replace negative values with 0.

Method 1. Using Traversal and List Comprehension.

Example

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
arr = [0 if x < 0 else x for x in arr]
arr = np.array(arr)
print("array with replaced values is: ", arr)

Output

array with replaced values is: [ 0 32 0 42 0 88]

Explanation

Here in the above example we iterate through each element in the NumPy array arr using list comprehension method. While traversing into the array we checked if the current value is negative (x<0) then we replace it with zero else we will not change it. After the modification we convert the array back to numpy array.

Method 2. Using Numpy.vectorize().

Example

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
def replace_negative_value(x):
   return 0 if x < 0 else x

replace_func = np.vectorize(replace_negative_value)
arr = replace_func(arr)
print("array with replaced values is: ", arr)

Output

array with replaced values is: [ 0 32 0 42 0 88]

Explanation

Here in the above example we used the function numpy.vectorize() which allows us to apply custom function on each element of the NumPy array. We define a function replace_negative_value() which will replace the negative values with zero. Then using the np.vectorize() method we created vectorized version of the function to apply it on an array.

Method 3. Using Numpy.clip().

Example

import numpy as np
arr = np.array([-12, 32, -34, 42, -53, 88])
arr = np.clip(arr, 0, None)
print("array with replaced values is: ", arr)

Output

array with replaced values is: [ 0 32 0 42 0 88]

Explanation

Here in the above example we used numpy.clip() method using which we can limit a value to any defined range. In our program we define the value 0 as lower bound and None in case upper bound so this can accept any larger positive value but in case of negative value it will replace the negative value as 0.

Method 4. Using Numpy.where().

Example

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
arr = np.where(arr < 0, 0, arr)
print("array with replaced values is: ", arr)

Output

array with replaced values is: [ 0 32 0 42 0 88]

Explanation

Here in the above example we used numpy.where() method using which we can provide any conditional replacement method. In our program we provided condition as arr<0 and we replaced the value of array with 0 where this condition will be true. In case the condition will not satisfy then the value will be unchanged.

Method 5. Using Numpy.maximum().

Example

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
arr = np.maximum(arr, 0)
print("array with replaced values is: ", arr)

Output

array with replaced values is: [ 0 32 0 42 0 88]

Explanation

Here in the above example we used the function numpy.maximum() which compares the element of two arrays and returns maximum among them. In our program we are comparing each element of array with 0 and selecting the maximum value which definitely replaces the negative as 0 will be maximum so 0 will be returned from the function and negative value will get replaced by 0.

So, we saw different approach using which we can replace any negative value of the array into 0. In this article we get to know about methods like traversal, list comprehension, mathematical operations which provide us efficient way to replace the negative value. You can choose any method which best fits your requirement and readability.

Updated on: 03-Oct-2023

992 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements