# [SOLVED] How to count the number of true elements in each row of a NumPy bool array

## Issue

I have a NumPy array ‘boolarr’ of boolean type. I want to count the number of elements whose values are True in each row. Is there a NumPy or Python routine dedicated for this task?

For example, consider the code below:

``````>>> import numpy as np
>>> boolarr = np.array([[0, 0, 1], [1, 0, 1], [1, 0, 1]], dtype=np.bool)
>>> boolarr
array([[False, False,  True],
[ True, False,  True],
[ True, False,  True]], dtype=bool)
``````

The count of each row would give the following results:

``````1
2
2
``````

## Solution

``````In : boolarr = np.array([[0, 0, 1], [1, 0, 1], [1, 0, 1]], dtype=bool)
In : boolarr
Out:
array([[False, False,  True],
[ True, False,  True],
[ True, False,  True]])
``````

Just use `sum`:

``````In : np.sum(boolarr, axis=1)
Out: array([1, 2, 2])
``````

The `True` count as `1` when doing addition.

Or:

``````In : np.count_nonzero(boolarr, axis=1)
Out: array([1, 2, 2])
``````