In this article, we will learn all about numpy.count_nonzero() function in python and see how to use it to count values based on conditions in 1D or 2D Numpy Arrays.
Table of Contents
 Overview of numpy.count_nonzero()
 Count nonzero values in a Numpy Array.
 Count True values in a Numpy Array.
 Count values in a Numpy Array based on conditions.
 Count nonzero values in complete 2D Numpy array or in each row / column.
 Count values in complete 2D Numpy array or in each row / column that satisfy a condition.
numpy.count_nonzero()
Numpy module in python provides a function to count nonzero values in array,
numpy.count_nonzero(arr, axis=None, keepdims=False)
Arguments:
 arr: array like object
 The Array in which we want to count the non zero values
 axis: int or tuple, optional
 Axis along which we want to count the values.
 If 1 then it will count non zero values in rows.
 If 0 then it will count non zero values in columns.
 If None then it will flatten the array and then count non zero values in it.
 Axis along which we want to count the values.
 keepdims: bool
 If True, them the axes that are counted are left in the result as dimensions with size one.
Returns:
 int or array of int
 Returns the count of non zero values in numpy array.
 If Axis is provided then returns the array of count of values along the axis.
In Python, True is equivalent to 1 and False is equivalent to 0. So, we can use can use count_nonzero() function to count values in numpy array that satisfy a condition. Let’s learn that step by step with examples.
Count non zero values in a Numpy Array
Suppose we have a numpy array of integers, which contains some zeros and some non zero values. To count the all the non zeros values in array, use the count_nonzero() function. For example,
import numpy as np # Create a numpy array from list arr = np.array([2, 3, 0, 5, 0, 0, 5, 0, 5]) # Count non zero elements in numpy array count = np.count_nonzero(arr) print('Count of nonzero values in NumPy Array: ', count)
Output:
Count of nonzero values in NumPy Array: 5
Count True values in a numpy Array
In Python, True is equivalent to 1 and False is equivalent to 0. So, we can use can use count_nonzero() function to count True values in a bool numpy array. For example,
import numpy as np # Create a Numpy Array of bool values arr = np.array([False, True, True, True, False, True, False, True, True]) # Count True elements in numpy array count = np.count_nonzero(arr) print('Count of True values in NumPy Array: ', count)
Output:
Count of True values in NumPy Array: 6
Now we will see why counting True values in a bool array is important.
Count Values in Numpy Array that satisfy a condition
When we apply a condition to a numpy array like arr > 3, then it returns a bool array of same size as arr. It contains True at places where the element in arr satisfies the condition i.e. greater than 3 in this case, all other values are False. So, if we count True values in bool array returned by arr>3, then it will give us the count of values that satisfies the condition in array i.e. values greater than 3 in this case. Let’s use this logic to count values in numpy array based on conditions. For example,
Count Even numbers in a Numpy Array
import numpy as np # Numpy array of numbers arr = np.array([2, 3, 1, 5, 4, 2, 5, 6, 5]) # Count even number of even elements in array count = np.count_nonzero(arr % 2 == 0) print('Count of Even Numbers in Numpy Array: ', count)
Output:
Count of Even Numbers in Numpy Array: 4
Count NonZero Values in 2D Numpy Array
Suppose we have a 2D Numpy array and we want to count all non zero values in it. To do that we can use count_nonzero() function with default value of axis parameter i.e. None. For example,
import numpy as np # Create 2D Numpy ARray arr_2d = np.array( [[2, 3, 0], [5, 0, 0], [5, 0, 5]]) # Get count of non zero values in complete 2D array count = np.count_nonzero(arr_2d) print('Count of non zero values in complete 2D array: ', count)
Output:
Count of non zero values in complete 2D array: 5
Count NonZero Values in each row of 2D Numpy Array
Suppose we have a 2D Numpy array and we want to count all non zero values in each row of it. To do that we can use count_nonzero() function with axis parameter as 1. For example,
import numpy as np # Create 2D Numpy ARray arr_2d = np.array( [[2, 3, 0], [5, 0, 0], [5, 0, 5]]) # Get count of non zero values in each row of 2D array count = np.count_nonzero(arr_2d, axis=1) print('Count of non zero values in each row of 2D array: ', count)
Output:
Count of non zero values in each row of 2D array: [2 1 2]
It returned an array containing count of non zero values in each row.
Count NonZero Values in each column of 2D Numpy Array
Suppose we have a 2D Numpy array and we want to count all non zero values in each column of it. To do that we can use count_nonzero() function with axis parameter as 0. For example,
import numpy as np # Create 2D Numpy ARray arr_2d = np.array( [[2, 3, 0], [5, 0, 0], [5, 0, 5]]) # Get count of non zero values in each column of 2D array count = np.count_nonzero(arr_2d, axis=0) print('Count of non zero values in each column of 2D array: ', count)
Output:
Count of non zero values in each column of 2D array: [3 1 1]
It returned an array containing count of non zero values in each column.
Count values in 2D Numpy array based on condition
To count all the values in 2D array that satisfy a condition, we can use the count_nonzero() function with different values of axis parameter
 axis=None, to count all values in 2D array that satisfy a condition.
 axis=1, to count all values in each row of 2D array that satisfy a condition.
 axis=0, to count all values in each column of 2D array that satisfy a condition.
For example,
import numpy as np # Create 2D Numpy ARray arr_2d = np.array( [[2, 3, 0], [5, 0, 0], [5, 0, 5]]) # Get count of even values in complete 2D array count = np.count_nonzero(arr_2d % 2 == 0) print('Count of even values in complete 2D array: ', count) # Get count of even values in each row of 2D array count = np.count_nonzero(arr_2d % 2 == 0, axis=1) print('Count of even values in each row of 2D array: ', count) # Get count of even values in each column of 2D array count = np.count_nonzero(arr_2d % 2 == 0, axis=0) print('Count of even values in each column of 2D array: ', count)
Output:
Count of even values in complete 2D array: 5 Count of even values in each row of 2D array: [2 2 1] Count of even values in each column of 2D array: [1 2 2]
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.