replace all nans in a numpy array with zeros

Numpy – Replace All NaN Values with Zeros

In this tutorial, we will look at how to replace all occurrences of NaN values in a Numpy array with zeros with the help of some examples.

How do I replace all NaN with 0 in Numpy?

replace all nans in a numpy array with zeros

Use boolean indexing to replace all instances of NaN in a Numpy array with zeros. Here, we use the numpy.isnan() function to check whether a value inside the array is NaN or not, and if it is, we set it to zero.

The following is the syntax –

import numpy as np
ar[np.isnan(ar)] = 0

Let’s now look at a step-by-step example of using the above syntax on a Numpy array.

Step 1 – Create a Numpy array

First, we will create a one-dimensional array that we will be using throughout this tutorial.

import numpy as np

# create numpy array
ar = np.array([1, 2, np.nan, 3, 4, np.nan, np.nan, 5])
# display the array
ar

Output:

array([ 1.,  2., nan,  3.,  4., nan, nan,  5.])

Here, we used the np.array() function to create a Numpy array with some numbers and some NaN values.

Step 2 – Set NaN values in the array to 0 using boolean indexing

Use the numpy.isnan() function to check whether a value in the array is NaN or not. If it is, set it to zero.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

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

Let’s replace all occurrences of NaN in the above array with 0.

# replace nan with zeros
ar[np.isnan(ar)] = 0
# display the array
ar

Output:

array([1., 2., 0., 3., 4., 0., 0., 5.])

You can see that each instance of NaN has been replaced by a 0 in the above array. Note that here we are modifying the original array.

You can also use this method to replace NaN values with 0s in higher-dimensional arrays. For example, let’s apply this method to a two-dimensional array containing some NaN values.

# create a 2D numpy array
ar = np.array([
    [1, np.nan, 2],
    [np.nan, 3, 4],
    [5, np.nan, np.nan]
])
# display the array
ar

Output:

array([[ 1., nan,  2.],
       [nan,  3.,  4.],
       [ 5., nan, nan]])

Here, we created a 2D Numpy array containing some NaN values.

Let’s now replace the NaN values in this 2D array with 0s.

# replace nan with zeros
ar[np.isnan(ar)] = 0
# display the array
ar

Output:

array([[1., 0., 2.],
       [0., 3., 4.],
       [5., 0., 0.]])

The array now has 0s in place of NaNs.

You can similarly use this method to replace NaN values in a Numpy array with any other value.

Summary – Replace NaN values in Numpy array with zeros

In this tutorial, we looked at how to replace all NaN values in a Numpy array with zeros. The following is a short summary of the steps mentioned in this tutorial.

  1. Create a Numpy array (skip this step if you already have an array to operate on).
  2. Use the numpy.isnan() function to check whether a value in the array is NaN or not. If it is, set it to 0 using boolean indexing ar[np.isnan(ar)] = 0

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top