“How can NaN values be replaced with zero in NumPy?”

How can NaN values be replaced with zero in NumPy?”

NumPy, or Numerical Python, is a popular library used for scientific computing in Python. It provides powerful tools for handling large multidimensional arrays and matrices. However, these arrays can sometimes contain missing or invalid values represented by NaN (Not a Number). In order to perform computations on these arrays, it is necessary to replace the NaN values with a suitable alternative. To address this issue, NumPy provides the function “numpy.nan_to_num()”, which replaces all NaN values with zero. This allows for seamless computation on the arrays without any errors or disruptions due to missing values. By using this function, users can effectively handle NaN values in their NumPy arrays and perform various operations on them with confidence.

Replace NaN Values with Zero in NumPy


You can use the following basic syntax to replace NaN values with zero in NumPy:

my_array[np.isnan(my_array)] = 0

This syntax works with both matrices and arrays.

The following examples show how to use this syntax in practice.

Example 1: Replace NaN Values with Zero in NumPy Array

The following code shows how to replace all NaN values with zero in a NumPy array:

import numpy as np

#create array of data
my_array = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])

#replace nan values with zero in array
my_array[np.isnan(my_array)] = 0

#view updated array
print(my_array)

[ 4.  0.  6.  0. 10. 11. 14. 19. 22.]

Notice that both NaN values in the original array have been replaced with zero.

Example 2: Replace NaN Values with Zero in NumPy Matrix

Suppose we have the following NumPy matrix:

import numpy as np

#create NumPy matrix
my_matrix = np.matrix(np.array([np.nan, 4, 3, np.nan, 8, 12]).reshape((3, 2)))

#view NumPy matrix
print(my_matrix)

[[nan  4.]
 [ 3. nan]
 [ 8. 12.]]

We can use the following code to replace all NaN values with zero in the NumPy matrix:

#replace nan values with zero in matrix
my_matrix[np.isnan(my_matrix)] = 0

#view updated array
print(my_matrix)

[[ 0.  4.]
 [ 3.  0.]
 [ 8. 12.]]

Notice that both NaN values in the original matrix have been replaced with zero.

Related:

The following tutorials explain how to perform other common tasks in NumPy:

Cite this article

stats writer (2024). How can NaN values be replaced with zero in NumPy?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-nan-values-be-replaced-with-zero-in-numpy/

stats writer. "How can NaN values be replaced with zero in NumPy?”." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-nan-values-be-replaced-with-zero-in-numpy/.

stats writer. "How can NaN values be replaced with zero in NumPy?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-nan-values-be-replaced-with-zero-in-numpy/.

stats writer (2024) 'How can NaN values be replaced with zero in NumPy?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-nan-values-be-replaced-with-zero-in-numpy/.

[1] stats writer, "How can NaN values be replaced with zero in NumPy?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can NaN values be replaced with zero in NumPy?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top