You can create a masked array like this:

data = np.array([[1,2,3], [4,5,np.NaN], [np.NaN,6,np.NaN], [0,0,0]])
masked_data = np.ma.masked_array(data, np.isnan(data))
# calculate your weighted average here instead
weights = [1, 1, 1]
average = np.ma.average(masked_data, axis=1, weights=weights)
# this gives you the result
result = average.filled(np.nan)
print(result)

This outputs:

[ 2.   4.5  6.   0. ]
Answer from Alex on Stack Overflow
Discussions

python - NumPy: calculate averages with NaNs removed - Stack Overflow
How can I calculate matrix mean values along a matrix, but to remove nan values from calculation? (For R people, think na.rm = TRUE). Here is my [non-]working example: import numpy as np dat = np... More on stackoverflow.com
🌐 stackoverflow.com
ENH: np.nanmean with weights
Proposed new feature or change: My motivation comes from this PR in sklearn https://github.com/scikit-learn/scikit-learn/pull/23183/files#diff-b62de604fe871c9d2dcbfc799daf93b5bcebef1bf08341a0a7fc75... More on github.com
🌐 github.com
4
April 21, 2022
BUG: average() returns nan when an `inf` value is given a 0 weight
Describe the issue: When I try to compute the weighted mean of an array that includes inf, and that element is given a weight of 0, I'm getting a RuntimeWarning, and a nan result. This isn'... More on github.com
🌐 github.com
12
December 10, 2023
numpy - nanmean with weights to calculate weighted average in pandas .agg - Stack Overflow
I'm using a lambda function in a pandas aggregation to calculate the weighted average. My issue is that if one of the values is nan, the whole result is nan of that group. How can I avoid this? df... More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.5 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
CSDN
devpress.csdn.net › python › 630463e97e6682346619af02.html
Taking np.average while ignoring NaN's? - DevPress官方社区
August 23, 2022 - data = np.array([[1,2,3], [4,5,np.NaN], [np.NaN,6,np.NaN], [0,0,0]]) masked_data = np.ma.masked_array(data, np.isnan(data)) # calculate your weighted average here instead weights = [1, 1, 1] average = np.ma.average(masked_data, axis=1, weights=weights) # this gives you the result result = average.filled(np.nan) print(result) ... 问题:如何重塑熊猫。系列 在我看来,它就像 pandas.Series 中的一个错误。 a = pd.Series([1,2,3,4]) b = a.reshape(2,2) b b 有类型 Series 但无法显示,最后一条语句给出异常,非常冗长,最后一行是“TypeError: %d format: a number is required, not numpy.ndarray”。 b.sha
🌐
GitHub
github.com › numpy › numpy › issues › 21375
ENH: np.nanmean with weights · Issue #21375 · numpy/numpy
April 21, 2022 - Wrapper for np.average, with np.nan values being ignored from the average This is similar to np.nanmean, but allowing to pass weights as in np.average · The ideas would be to add this functionality to one of the two functions, i.e: a) add weights to np.nanmean: https://numpy.org/doc/stable/reference/generated/numpy.nanmean.html b) add option to ignore nan's for np.average : https://numpy.org/doc/stable/reference/generated/numpy.average.html ·
Author: numpy
🌐
AlgoCademy
algocademy.com › blog › understanding-np-mean-a-comprehensive-guide-to-calculating-averages-in-numpy
Understanding np.mean: A Comprehensive Guide to Calculating Averages in NumPy – AlgoCademy Blog
To handle NaN values, NumPy provides np.nanmean(), which ignores NaN values when computing the mean: # Using nanmean to ignore NaN values nan_mean = np.nanmean(arr_with_nan) print(f"Mean ignoring NaNs: {nan_mean}") # Output: 3.0 · Sometimes, not all values in your data should contribute equally ...
🌐
Codecademy
codecademy.com › article › hands-on-statistics-with-numpy-in-python
Hands-on Statistics with NumPy in Python | Codecademy
Similarly, we get a nan value at the third position in the output array while calculating the weighted mean for elements in each column. NumPy doesn’t have a function like nanaverage() to ignore nan values and calculate the weighted mean for the rest of the values in the input arrays.
Find elsewhere
🌐
GitHub
github.com › numpy › numpy › issues › 25362
BUG: average() returns nan when an `inf` value is given a 0 weight · Issue #25362 · numpy/numpy
December 10, 2023 - When I try to compute the weighted mean of an array that includes inf, and that element is given a weight of 0, I'm getting a RuntimeWarning, and a nan result. This isn't the case when the weight is just slightly larger than zero. Drilling this down, I reached np.multiply(), but I'm not familiar enough with python/C to dig deeper.. import sys import numpy as np print(np.average([1, 1], weights=[1, 0])) # 1.0 print(np.average([np.inf, 1], weights=[1, 1])) # inf print(np.average([np.inf, 1], weights=[1, 0])) # inf print(np.average([np.inf, 1], weights=[0, 1])) # nan # RuntimeWarning: invalid value encountered in multiply avg = np.multiply(a, wgt, eps = sys.float_info.epsilon print(np.average([np.inf, 1], weights=[0+eps, 1])) # inf ·
Author: numpy
🌐
Medium
medium.com › @heyamit10 › how-to-use-numpy-nanmean-to-calculate-the-mean-while-ignoring-nan-f5da3320be87
How to use numpy.nanmean() to Calculate the Mean While Ignoring nan | by Hey Amit | Medium
February 8, 2025 - Including those missing scores as zeros would skew the results. Ignoring them, on the other hand, gives you a fair average. That’s exactly the kind of problem we face with nan values in NumPy arrays.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.2 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v1.9 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
Kodeclik
kodeclik.com › python-average-with-nan-values
Find mean in numpy ignoring NaN values
October 30, 2024 - This example creates an array with explicit NaN values using the “np.nan” notation, and then computes the average by ignoring the NaN values.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy nanmean() – get mean ignoring nan values
NumPy nanmean() - Get Mean ignoring NAN Values - Spark By {Examples}
March 27, 2024 - Python NumPy nanmean() function is used to compute the arithmetic mean or average of the array along a specified axis while ignoring NaN (Not a Number)
🌐
Saturn Cloud
saturncloud.io › blog › calculating-averages-of-multiple-columns-ignoring-nan-a-guide-for-data-scientists
Calculating Averages of Multiple Columns Ignoring NaN A Guide for Data Scientists | Saturn Cloud Blog
May 1, 2026 - By setting the skipna=True parameter in the mean() function in pandas or using the nanmean() function in numpy, we can easily calculate the average of multiple columns while ignoring NaN values.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-156.php
Python NumPy: Calculate averages without NaNs along a given array - w3resource
August 29, 2025 - print(result.filled(np.nan)): Replace the masked values in 'result' with NaN using the filled() method and print the final result. This will display the row-wise mean of 'arr1', excluding the NaN values.
🌐
Somethings Blog
somethingsblog.com › home › master weighted averages in numpy: a step-by-step guide
Master Weighted Averages in NumPy: A Step-by-Step Guide - Somethings Blog
March 7, 2025 - By understanding how to use NumPy’s average() method effectively, you can unlock the power of weighted averages and take your data analysis to the next level. ... Mastering numpy.nanmean(): Ignore NaNs with EaseComputing the arithmetic mean while ignoring NaNs (Not a Number) is a breeze with numpy.nanmean(). Learn its syntax, arguments, and return value in this comprehensive guide, complete with examples to get you started.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.1 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.