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.
🌐
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
🌐
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 ...
🌐
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)
🌐
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).
🌐
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.
🌐
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, ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › 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
April 25, 2022 - Example Input: [ nan -inf  5.] Output: [0.00000e+00 9.99999e+05 5.00000e+00] Explanation: Replacing NaN with 0 and negative inf with any value. numpy.nan_to_num method The numpy.nan_ ... Given numpy array, the task is to replace negative value with zero in numpy array. Let’s see a few examples of this problem. Method #1: Naive Method Python3 # Python code to demonstrate # to replace negative value with 0 import numpy as np ini_array1 = np.array([1, 2, -3, 4, -5, -6]) # printing init
🌐
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 › numpy-nan_to_num-in-python
numpy.nan_to_num() in Python - GeeksforGeeks
November 28, 2018 - numpy.nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array.
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-fillna-equivalent-in-numpy-1caafd79145b
Understanding numpy.fillna Equivalent in NumPy | by Hey Amit | Medium
February 8, 2025 - Here, NaN values represent the missing data in the array. ... Replacing NaN values with 0 or another constant value is often the simplest way to clean your data.
🌐
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 26, 2026 - This function replaces NaN with zero and infinity values with large finite numbers, making your data suitable for mathematical operations. numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None) Let's create an array with NaN and infinity values and clean them ?
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.0 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.