Python:NumPy .nanmean()
Published May 14, 2025
Contribute to Docs
In NumPy, the .nanmean() function computes the arithmetic mean of the elements in an array over a specified axis, while ignoring NaN (Not a Number) values. This is useful when working with arrays that contain missing or undefined values represented as NaN.
Syntax
numpy.nanmean(a, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)
Parameters:
a: The data for which the mean is computed. It can be a NumPy array or any array-like object (e.g., list).axis(Optional): Used to specify the axis along which the mean is calculated. IfNone(default), the mean is computed over the flattened array.dtype(Optional): The data type used for the calculation. If not specified, the result will have the same data type as the input (unlessais an integer, where the result will befloat64by default).out(Optional): A location where the result is stored. The array must have the same shape as the expected output. If not provided, a new array is created.keepdims(Optional): If set toTrue, the reduced axes are retained in the result with size one. This can be useful for broadcasting purposes.where(Optional): A condition that allows the function to compute the mean only on elements where the condition isTrue. It can be a boolean array or a condition (usuallyTrue).
Return value:
The .nanmean() function returns the mean of the array, ignoring NaN values:
- If
axisisNoneor if no axis is specified, it returns a single value representing the mean of the entire array. - If
axisis specified, it returns an array of mean values along the specified axis of the dataa.
Example 1: Basic Usage of .nanmean()
The example below shows how to calculate the mean value from an array:
import numpy as np# Creating a numpy array with NaN valuesarr = np.array([6, 13, 1, np.nan, 7])# Computing the mean of the array, ignoring NaNsresult = np.nanmean(arr)print(result)
The example code above results in the following output:
6.75
Example 2: Using .nanmean() Along an Axis
This example shows how to use .nanmean() function along a specific axis:
import numpy as np# Creating a 2D numpy array with NaN valuesarr = np.array([[8, np.nan, 3], [4, 2, 9]])# Computing the mean along axis 0 (columns)result = np.nanmean(arr, axis=0)print(result)
The example code above results in the following output:
[6. 2. 6.]
Codebyte Example: Using .nanmean() with keepdims
In this codebyte example, the .nanmean() method computes the mean of the elements in the array, demonstrating the use of keepdims parameter:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours