NumPy nanmean() – Get Mean ignoring NAN Values

Python NumPy nanmean() function is used to compute the arithmetic mean or average of the array ignoring the NaN value. If the array has a NaN value and we can find out the average without being influenced by the NaN value. The mean/average is taken over the flattened array by default, otherwise over the specified axis.

In this article, I will explain how to use numpy.nanmean() function in Python to return the average of the array elements by ignoring NaN with examples.

1. Quick Examples of Python NumPy nanmean() Function

If you are in a hurry, below are some quick examples of how to use nanmean() function in NumPy Python.


# Below are a quick example

Example 1: create 2-d array with nan value
arr = np.array([[8, np.nan, 16], [6, 12, 25]])
# Get the nanmean of 2-D array
arr2 = np.nanmean(arr)

Example 2:Use mean() array without nanmean() function 
arr2 = np.mean(arr)

Example 3:create 2-d matrix with nan value
arr = np.array([[24, 32, 85], [57, np.nan, 16], [8, 17, np.nan], [43, 78, 39]])
# Get the nanmean values over row for each of 3 columns 
arr2 = np.nanmean(arr, axis = 0)

Example 4: Get the mean values over row for each of 3 columns
arr2 = np.mean(arr, axis = 0)

Example 5: Get the nanmean values over column for each of 4 rows 
arr2 = np.nanmean(arr, axis = 1)

Example 6: Get the mean values over column for each of 4 rows 
arr2 = np.mean(arr, axis = 1)

2. Syntax of numpy.nanmean() Function

Following is the syntax of numpy.nanmean() function.


# Syntax of numpy.nanmean()
numpy.nanmean(arr, axis=None, dtype=None, out=None, keepdims=))

2.1 Parameter of numpy.nanmean()

Allows following parameters.

  • arr : Array containing numbers whose mean is desired. If arr is not an array, a conversion is attempted.
  • axis : Axis or axes along which the means are computed. we can use axis=1 means row wise or axis=0 means column wise.
  • dtype : Type to used during the calculation of the arithmetic mean. For integer inputs, the default is float64.
  • out : Alternate output array in which to place the result.
  • keepdims : If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.

2.2 Return Value of numpy.nanmean()

It returns the average of the array elements, otherwise, a reference to the output array is returned. Nan is returned for slices that contain only NaNs.

3. Usage of NumPy nanmean() Function

Use numpy.nanmean() function is used to get the mean value in an array containing one or more NaN values. It calculates the mean by taking into account only the non-NaN values in the array.

The numpy.nanmean() function is very similar to the numpy.mean() function in its arguments. When we use numpy.nanmean() function is used to calculate the mean of a Numpy array containing NaN values. By not specifying the axis, it will return the mean of all the values inside the array.


import numpy as np

# create 2-d array with nan value
arr = np.array([[8, np.nan, 16], [6, 12, 25]])

# Get the nanmean of 2-D array
arr2 = np.nanmean(arr)
print(arr2)

# Output
# 13.4

# Use mean() array without nanmean() function 
arr2 = np.mean(arr)
print(arr2)

# Output
# nan

4. Get the nanmean() Values of 2-D Array along Axis = 0

We can calculate the mean value of an array by ignoring NaN along with a specified axis using numpy.nanmean() function. Use axis=0 param to get the mean of each column in the array.


import numpy as np

# create 2-d matrix with nan value
arr = np.array([[24, 32, 85],
                [57, np.nan, 16],  
                [8, 17, np.nan],
                [43, 78, 39]])

# Get the nanmean values over row for each of 3 columns 
arr2 = np.nanmean(arr, axis = 0)
print(arr2)

# Output
# [33.   42.33333333 46.66666667]

# Get the mean values over row for each of 3 columns
arr2 = np.mean(arr, axis = 0)
print(arr2)

# Output
# [33. nan nan]

5. Get the nanmean() Values of 2-D Array along Axis = 1

We can also use the np.nanmean() function with axis=1 to get the mean value for each row in the array.


# Get the nanmean values over column for each of 4 rows 
arr2 = np.nanmean(arr, axis = 1)
print(arr2)

# Output
# [47.    36.5     12.5     53.33333333]

# Get the mean values over column for each of 4 rows 
arr2 = np.mean(arr, axis = 1)
print(arr2)

# Output
# [47.     nan    nan   53.33333333]

6. Conclusion

In this article, I have explained how to use NumPy nanmean() function in Python which calculates the arithmetic mean/average along with the specified axis and by ignoring NaN values with examples.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing NumPy nanmean() – Get Mean ignoring NAN Values