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
🌐
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 - Modified: 2024-01-23 | Tags: Python, NumPy · 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 replace ...
🌐
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.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.2 Manual
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
🌐
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 this blog, we focus on 0/0 as a key scenario, but the methods below apply to replacing NaN values from any source. Replacing NaN with zero is a common workaround to salvage datasets and keep calculations running. Below are the most effective methods using Python’s NumPy (for arrays) and pandas (for tabular data).
🌐
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 values will be substituted with 0.0 if no value is given. posinf: optional value, int or float. Fill positive infinity values with this value. Positive infinity values will be replaced with an extremely big number if no value is given. neginf: ...
🌐
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
Combined with conditional indexing, this method allows us to selectively replace NaN values with zeros. ... 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 NaN values with zero array_with_nans[np.isnan(array_with_nans)] = 0 # Print the modified array print(array_with_nans)
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.
🌐
AskPython
askpython.com › python-modules › numpy › replacing-nan-infinity-python
Usage of nan_to_num in replacing NaN and Infinity - AskPython
February 23, 2023 - It is just that since np.nan is a special floating-point value in Python. NaN has a floating-point data type associated with it. And since the primary rule of elements in an array is that they all should have the same datatype, all the elements are converted to float. Let us see the replacement 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)
🌐
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 ...
🌐
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.
🌐
w3resource
w3resource.com › python-exercises › numpy › handling-nan-values-in-numpy-arrays-using-np-dot-nan_to_num.php
Handling NaN values in NumPy arrays using np.nan_to_num
4. 0. 6.] ... Import the NumPy library to handle array operations. ... Define a NumPy array array_with_nan that includes some NaN values. ... Use "np.nan_to_num()" to replace NaN values in the array with a specified number (e.g., 0.0).
🌐
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, ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.1 Manual
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
🌐
Python Help
pythonhelp.org › numpy › how-to-replace-nan-values-in-numpy-array
Replacing Nan Values in Numpy Array
Here’s a step-by-step approach to replacing NaN values in numpy arrays: ... In this example, we used np.nan_to_num() to replace all NaN values with 0.
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-nan-values-with-zeros-in-pandas-dataframe
Replace NaN Values with Zeros in Pandas DataFrame - GeeksforGeeks
Syntax to replace NaN values with zeros of a single column in Pandas dataframe using fillna() function is as follows: Syntax: df['DataFrame Column'] = df['DataFrame Column'].fillna(0)
Published: July 15, 2025
🌐
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))...