In this article, we will discuss different ways to count True elements in a bool Numpy Array.
Table of Contents
 Use count_nonzero() to count True elements in NumPy array.
 Use sum() to count True elements in a NumPy array.
 Use bincount() to count True elements in a NumPy array.
 Count True elements in 2D Array.
 Count True elements in each row of 2D Numpy Array / Matrix.
 Count True elements in each column of 2D Numpy Array / Matrix.
Use count_nonzero() to count True elements in NumPy array
In Python, False is equivalent to 0 , whereas True is equivalent to 1 i.e. a nonzero value.
Numpy module provides a function count_nonzero(arr, axis=None), which returns the count of non zero values in a given numpy array. When the value of axis argument is None, then it returns the count of non zero values in the complete array. But in case you are dealing with a multidimensional array, then you can use the axis argument to count non zero values along the given axis.
As non zero values are equivalent to True, so we can use the count_nonzero() to count True elements in a numpy array. For example,
import numpy as np arr = np.array([False, True, True, True, False, True, False, True, True]) print('Numpy Array:') print(arr) # Get count of True elements in a numpy array count = np.count_nonzero(arr) print('Print count of True elements in array: ', count)
Output:
Print count of True elements in array: 6
count_nonzero() returned the count of True elements in the numpy array.
Use sum() to count True elements in a NumPy array
As True values are equivalent to 1 in Python. So, we can also add all the True values in a numpy array to get the count of True elements in a numpy array. For example,
import numpy as np arr = np.array([False, True, True, True, False, True, False, True, True]) # Get count of True elements in a numpy array count = np.sum(arr) print('Print count of True elements in array: ', count)
Output:
Print count of True elements in array: 6
It returned the count of all True elements in the array.
Use bincount() to count True elements in a NumPy array
In python, the numpy module provides a function bincount(arr), which returns a count of number of occurrences of each value in array of nonnegative ints.
bincount(arr), returned a result array, where ith element contains the occurence of i in arr. For example,
 result[1] contains the occurrences of 1 in array
 result[2] contains the occurrences of 2 in array
 result[3] contains the occurrences of 3 in array
As True is equivalent to 1 in python, so in the array returned by bincount() the element at index 1 contains the count of True elements in numpy array. For example,
import numpy as np arr = np.array([False, True, True, True, False, True, False, True, True]) # Get count of occurence of each value in numpy array of nonnegative ints bin_arr = np.bincount(arr) # Get count of True elements in a numpy array count = bin_arr[1] print('Print count of True elements in array: ', count)
Output:
Print count of True elements in array: 6
Count True elements in 2D Array
We can use the count_nonzero() with default value of axis parameter i.e. None, to get the count of all non zero values or True elements in complete 2D Numpy array. For example,
import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) print('Matrix / 2D Array:') print(arr_2d) # Count of True in complete 2D Numpy array count = np.count_nonzero(arr_2d) print('Print count of True elements in complete 2D array: ', count)
Output:
Print count of True elements in complete 2D array: 6
Count True elements in each row of 2D Numpy Array / Matrix
To count the true elements in each row of a 2D Numpy Array or Matrix, pass the axis parameter as 1. For example,
import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) print('Matrix / 2D Array:') print(arr_2d) # Count of True elements in each row of 2D Numpy Array count = np.count_nonzero(arr_2d, axis=1) print('Print count of True elements in each row pf the 2D array: ', count)
Output:
Print count of True elements in each row pf the 2D array: [2 2 2]
It returned an array containing the count of True elements in each row of the original 2D array.
Using sum() function:
We can also use sum() to add the True values in each row of a 2D Numpy array. For that we need to pass the axis parameter as 1. For example,
import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) # Count of True elements in each row of 2D Numpy Array count = np.sum(arr_2d, axis=1) print(count)
Output:
[2 2 2]
It returned an array containing the count of True elements in each row of the original 2D array.
Count True elements in each column of 2D Numpy Array / Matrix
To count the true elements in each column of a 2D Numpy Array or Matrix, pass the axis parameter as 0. For example,
import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) # Count of True elements in each column of 2D Numpy Array count = np.count_nonzero(arr_2d, axis=0) print('Print count of True elements in each column of the 2D array: ', count)
Output:
Print count of True elements in each column of the 2D array: [1 2 3]
It returned an array containing the count of True elements in each column of the original 2D array.
Using sum() function:
We can also use sum() to add the True values in each column of a 2D Numpy array. For that we need to pass the axis parameter as 0. For example,
import numpy as np # Create a 2D Array of shape 3X3 arr_2d = np.array([ [False, True, True], [True, False, True], [False, True, True]]) # Count of True elements in each column of 2D Numpy Array count = np.sum(arr_2d, axis=0) print(count)
Output:
[1 2 3]
It returned an array containing the count of True elements in each column of the original 2D array.
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.