Skip to Content

Numpy – Count Zeros in Array with Examples

In this tutorial, we will look at how to count zeros in a numpy array. We will also look at how to count zeros present in each row and each column of a 2d array.

You can use np.count_nonzero() or the np.where() functions to count zeros in a numpy array. In fact, you can use these functions to count values satisfying any given condition (for example, whether they are zero or not, or whether they are greater than some value or not, etc).

Note that using np.count_nonzero() is simpler of the two methods. The following is the syntax to count zeros using this function –

# arr is a numpy array
# count of zeros in arr 
n_zeros = np.count_nonzero(arr==0)

Let’s look at some examples of how to use the above functions. First, we will create a couple of numpy arrays that we will be using throughout this tutorial.

import numpy as np

# one-dimensional array
arr_1d = np.array([3, 0, 5, 2, 1, 0, 8, 6])
print(arr_1d)

# two-dimensional array
arr_2d = np.array([[4, 3, 0],
                   [0, 0, 2],
                   [2, 5, 6]])
print(arr_2d)

Output:

[3 0 5 2 1 0 8 6]
[[4 3 0]
 [0 0 2]
 [2 5 6]]

Now we have a one-dimensional array and a two-dimensional array for which we will be counting the zeros.

To count all the zeros in an array, simply use the np.count_nonzero() function checking for zeros. It returns the count of elements inside the array satisfying the condition (in this case, if it’s zero or not).

Let’s use this function to count the zeros in arr_1d created above:

# count zeros in 1d array
n_zeros = np.count_nonzero(arr_1d==0)
# display the count of zeros
print(n_zeros)

Output:

2

We get 2 as the output since there are two zero elements in the 1d array arr_1d.

You can also use the same syntax to count zeros in higher dimensional arrays. Let’s count the number of zeros in arr_2d using np.count_nonzero()

# count zeros in 2d array
n_zeros = np.count_nonzero(arr_2d==0)
# display the count of zeros
print(n_zeros)

Output:

3

We get 3 as the output since there are three zero value elements in the array arr_2d.

To count zeros in each row, pass axis=1 to the np.count_nonzero() function. Let’s count zeros in each row of arr_2d

# count zeros in each row
n_zeros = np.count_nonzero(arr_2d==0, axis=1)
# display the count of zeros
print(n_zeros)

Output:

[1 2 0]

It returns a numpy array of the count of zeros in each row. You can see that we have one zero-element in the first row, two in the second row, and no such elements in the third row.

To count zeros in each column, pass axis=0 to the np.count_nonzero() function. Let’s count the zeros in each column of arr_2d

# count zeros in each column
n_zeros = np.count_nonzero(arr_2d==0, axis=0)
# display the count of zeros
print(n_zeros)

Output:

[1 1 1]

We have one zero-element in each column of the array arr_2d.

For more on the np.count_nonzero() function, refer to its documentation.

Alternatively, you can use np.where() to count the zeros in an array. np.where() is generally used to find indexes of elements satisfying a condition in a numpy array.

You can use this function to find indexes of zero-valued elements in the array and then count them to get the count of zeros in the array. Let’s count the zeros in the array arr_1d using this method:

# count zeros with np.where
result = np.where(arr_1d==0)
# show the result of np.where
print(result)

# count of zeros
n_zeros = result[0].size
# display the count of zeros
print(n_zeros)

Output:

(array([1, 5], dtype=int64),)
2

You can see that np.where() results in a tuple of numpy arrays showing the indexes satisfying the condition. We see that zeros are present at index 1 and 5 in the array arr_1. To get the count, we use the .size attribute of this index array.

You can also use np.where() to count zeros in higher-dimensional arrays as well. For example, let’s use it to count zeros in arr_2d

# count zeros with np.where
result = np.where(arr_2==0)
# show the result of np.where
print(result)

# count of zeros
n_zeros = result[0].size
# display the count of zeros
print(n_zeros)

Output:

(array([0, 1, 1], dtype=int64), array([2, 0, 1], dtype=int64))
3

The returned value from np.where() is a tuple of two arrays, the first one shows the row indexes of elements matching the condition (element equal to zero) and the second array gives the column indexes for those elements. Counting the indexes in any of these arrays gives the count of zeros in the array.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.