The Numpy library in Python comes with a number of useful functions and methods to work with and manipulate the data in arrays. In this tutorial, we will look at how to make all the positive values zero in a Numpy array with the help of some examples.
Steps to replace positive values with zero in Numpy
You can use boolean indexing to make all the positive values in a Numpy array zero. The following is the syntax –
# set positve values to zero ar[ar > 0] = 0
It replaces the positive values in the array
Let’s now look at a step-by-step example of using this syntax –
Step 1 – Create a Numpy array
First, we will create a Numpy array that we will use throughout this tutorial.
import numpy as np # create a numpy array ar = np.array([-2, -1, 0, 1, 2, 3, -4]) # display the array print(ar)
[-2 -1 0 1 2 3 -4]
Here, we used the
numpy.array() function to create a one-dimensional Numpy array containing some numbers. You can see that the array has both positive and negative values (along with a zero).
Step 2 – Set positive values to zero using boolean indexing
Using boolean indexing identify the values that are greater than zero and then set them to zero using the equality operator (
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
First, we will specify our boolean expression
ar > 0 and then set the array values satisfying this condition to zero.
For example, let’s replace all the positive values in the above array with zeros.
# set positive values to zero ar[ar > 0] = 0 # display the array print(ar)
[-2 -1 0 0 0 0 -4]
We replace all the positive values in the above array with zeros.
To understand what’s happening here, let’s look under the hood. Let’s see what we get from the expression
ar > 0
# create a numpy array ar = np.array([-2, -1, 0, 1, 2, 3, -4]) # result of boolean expression ar > 0 ar > 0
array([False, False, False, True, True, True, False])
We get a boolean array. The boolean values in this array represent whether a value at a particular index satisfies the given condition or not (in our case whether the element is greater than 0 or not).
When we do
ar[ar > 0] = 0, we are essentially setting the values in the array where the condition evaluates to
True to 0.
You can similarly filter a Numpy array for other conditions as well.
Summary – Make positive values zero in Numpy
In this tutorial, we looked at how to replace all the positive values in a Numpy array with zeros. The following is a short summary of the steps mentioned –
- Create a Numpy array (skip this step if you already have an array to operate on).
- Use boolean indexing to find the positive values and then set them to zero –
ar[ar > 0] = 0
You might also be interested in –
- Numpy Array – Get All Values Smaller than a Given Value
- Filter a Numpy Array – With Examples
- Extract the Last N Elements of Numpy Array
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.