Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Counting the number of non-NaN elements in a NumPy Array

  • Last Updated : 17 Oct, 2021

In this article, we are going to see how to count the number of non-NaN elements in a NumPy array in Python.

NAN: It is used when you don’t care what the value is at that position. Maybe sometimes is used in place of missing data, or corrupted data. 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Method 1: Using Condition

In this example, we will use one-dimensional arrays. In the below-given code, we loop over every entry of the given NumPy array and check if the value is a NaN or not.


import numpy as np
ex1 = np.array([1, 4, -9, np.nan])
ex2 = np.array([1, 45, -2, np.nan, 3
                -np.nan, 3, np.nan])
def approach_1(data):
    # here the input data, is a numpy ndarray
    # initialize the number of non-NaN elements 
    # in data
    count = 0       
    # loop over each entry of the data
    for entry in data:          
          # check whether the entry is a non-NaN value
        # or not
        if not np.isnan(entry):     
              # if not NaN, increment "count" by 1
            count += 1              
    return count



Method 2: Using isnan()

Using the functionality of NumPy arrays, that we can perform an operation on the whole array at once, instead of a single element.

Used function:

  • np.isnan(data): Returns a boolean array after performing np.isnan() operation on of the entries of the array, data
  • np.sum(): Since we are inputting a boolean array to the sum function, it returns the number of True values (1s) in the bool array.


import numpy as np
ex3 = np.array([[3, 4, -390, np.nan], 
                [np.nan, np.nan, np.nan, -90]])
def approach_2(data):
    return np.sum(~np.isnan(data))



Method 3:  Using np.count_nonzero() function

numpy.count_nonzero() function counts the number of non-zero values in the array arr.

Syntax : numpy.count_nonzero(arr, axis=None)

Parameters :
arr : [array_like] The array for which to count non-zeros.
axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of arr.

Return : [int or array of int] Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.


import numpy as np
ex4 = np.array([[0.35834379, 0.67202438, np.nan, np.nan,
                 np.nan, 0.47870971],
                [np.nan, np.nan, np.nan, 0.08113384,
                 0.70511741, 0.15260996],
                [0.09028477, np.nan, 0.16639899,
                    0.47740582, 0.72591160.94797347],
                [0.80305651,     np.nan, 0.67949724,
                    0.84112054, 0.15951702, 0.07510587],
                [0.28643337, 0.00804256, 0.36775056,
                 0.19360266, 0.07288145, 0.37076932]])
def approach_3(data):
    return data.size - np.count_nonzero(np.isnan(data))



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!