Where A is your 2D array:

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

The function isnan produces a bool array indicating where the NaN values are. A boolean array can by used to index an array of the same shape. Think of it like a mask.

Answer from Paul on Stack Overflow
🌐
Finxter
blog.finxter.com › 5-best-ways-to-replace-nan-with-zero-in-python-numpy-arrays
5 Best Ways to Replace NaN with Zero in Python Numpy Arrays – Be on the Right Side of Change
The numpy.nan_to_num() function is specifically designed to replace NaN values with zero in a numpy array in one step, greatly simplifying the process. ... import numpy as np # Create a numpy array with NaN values array_with_nans = np.array([1.0, np.nan, 2.5, np.nan, 5.0]) # Replace NaNs with ...
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-replace-nan-value-with-zero-using-numpy-
How to replace nan value with zero using numpy ? - Moonbooks
May 16, 2023 - It also has an optional parameter that can be used to specify the replacement value for nan values instead of using zero, which can be useful in certain situations. To use this function, simply pass in the array containing the nan values as an argument. For example ... array([[ 0.
🌐
pythontutorials
pythontutorials.net › blog › replace-the-nan-value-zero-after-an-operation-with-arrays
How to Replace NaN Values with Zero in Arrays After Operations: Fixing 0/0 Results — pythontutorials.net
For example: import numpy as np result = 0 / 0 # Returns NaN print(result) # Output: nan · This behavior is intentional, but when working with arrays (e.g., calculating ratios across multiple data points), 0/0 can introduce NaN values that ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan() | note.nkmk.me
January 23, 2024 - In NumPy, to replace NaN (np.nan) in an array (ndarray) with any values like 0, use np.nan_to_num(). Additionally, while np.isnan() is primarily used to identify NaN, its results can be used to replac ...
🌐
AskPython
askpython.com › python-modules › numpy › replacing-nan-infinity-python
Usage of nan_to_num in replacing NaN and Infinity - AskPython
February 23, 2023 - And since the primary rule of elements ... of nan values. # Replace NaN with 0 new_arr = np.nan_to_num(arr, nan=0.0) print("\nArray with NaN replaced by 0:\n", new_arr)...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-numpy-replace-nan-with-zero-and-fill-positive-infinity-for-complex-input-values
Python NumPy - Replace NaN with zero and fill positive infinity for complex input values - GeeksforGeeks
July 23, 2025 - [nan+infj] Shape of the array is : (1,) The dimension of the array is : 1 Datatype of our Array is : complex128 After replacement the array is : [0.+999999.j] In this example, we are replacing nan with the value of 100.
Find elsewhere
🌐
Data Science Parichay
datascienceparichay.com › home › blog › numpy – replace all nan values with zeros
Numpy - Replace All NaN Values with Zeros - Data Science Parichay
July 5, 2022 - Use boolean indexing to replace all instances of NaN (which you can check using the numpy.isnan() function) in a Numpy array with zeros.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-nan_to_num-in-python
numpy.nan_to_num() in Python - GeeksforGeeks
July 10, 2025 - numpy.nan_to_num() function replaces NaN (Not a Number) with a specified numerical value (default is 0), and optionally converts positive or negative infinity to finite numbers. Example: Python · import numpy as np a = np.array([1.0, np.nan, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-nan-with-zero-and-fill-negative-infinity-values-in-python
Replace NaN with zero and fill negative infinity values in Python - GeeksforGeeks
July 23, 2025 - # import package import numpy as ... is replaced with 999999 print("After replacement the array is : ", np.nan_to_num(array,nan= 0, neginf=999999)) ... In this example, we are creating an array with the help of complex numbers ...
🌐
TutorialsPoint
tutorialspoint.com › article › replace-nan-with-zero-and-fill-negative-infinity-values-in-python
Replace NaN with zero and fill negative infinity values in Python
March 1, 2022 - Original Array: [ inf -inf nan 5.5 -3.2] With Custom Values: [ 1.00000000e+03 -1.00000000e+03 9.99000000e+02 5.50000000e+00 -3.20000000e+00]
🌐
bioinfo core
bioinfocore.com › home › python › python numpy replace nan in array to 0 or a number
numpy array replace nan inf to 0 or number | bioinfo core
October 21, 2021 - numpy, Python · Replace nan in a numpy array to zero or any number: a = numpy.array([1,2,3,4,np.nan]) # if copy=False, the replace inplace, default is True, it will be changed to 0 by default a = numpy.nan_to_num(a, copy=True) # if you want ...
🌐
TutorialsPoint
tutorialspoint.com › replace-nan-with-zero-and-fill-positive-infinity-values-in-python
Replace NaN with zero and fill positive infinity values in Python
To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python − · print(" Result... ",np.nan_to_num(arr, posinf = 22222)) import numpy as np # Creating a numpy array using the array() method arr = np.array([np.inf, -np.inf, np.nan, -128, 128]) # Display the array print("Our Array...
🌐
Pythontutorials
pythontutorials.net › blog › numpy-replace-nan-with-0
Mastering `numpy`: Replacing `NaN` Values with 0 | PythonTutorials.net
June 21, 2025 - This can save unnecessary computation if the array doesn’t contain any NaN values. Replacing NaN with 0 might not always be the best approach.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-nan_to_num-in-python
numpy.nan_to_num() in Python - GeeksforGeeks
November 28, 2018 - If arr is inexact, then NaN is replaced by zero, and infinity (-infinity) is replaced by the largest (smallest or most negative) floating point value that fits in the output dtype.
🌐
IncludeHelp
includehelp.com › python › convert-nan-value-to-zero-in-numpy-array.aspx
Python - Convert nan value to zero in NumPy array
Suppose we are given a NumPy array with some nan values and we want to replace these nan values with zeroes. For this purpose, we will use the isnan() method which produces a bool array indicating where the NaN values are. A boolean array can be used to index an array of the same shape.