# NumPy count elements by condition

In this post, we are going to learn how to count NumPy array elements by the condition in Python for example. We will use the built-in functions np.any(), np.all(), np.sum(), np.where(), np.isnan(), np.inf(), count_nonzero() of the NumPy library

### 1. Numpy Count elements by condition mutiple

The ndarray comparison operation returns a boolean array of True and False. Where True is equal to 1 or False is equal is 0. In the below example the condition operation on ndarray returns a boolean array of TRUE and False. We have used np. sum() to count the number of elements satisfying the condition or return True.Instead of np.sum() the count_nonzero() is faster to count the number of element return True

• To get the elements to count column-wise we will pass axis=0
• To get the elements to count row-wise We will pass axis =1.
```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

print((nparr < 7) | (nparr % 3 == 0))
print('column-wise:',np.sum((nparr < 7) | (nparr % 3 == 0),axis=0))
print('rows-wise:',np.sum((nparr < 7) | (nparr % 3 == 0),axis=1))

```

Output

```[[ True  True  True  True  True]
[ True False False  True False]
[False  True False False  True]]
column-wise: [2 2 1 2 2]
rows-wise: [5 2 2]

```

### 2. Numpy Count elements by condition

The count_nonzero() returns the count of elements of the numpy array for a condition that returns True. Where True is equal to 1 or False is equal is 0.

If we do not pass the axis in count_nonzero() returns the count of elements in the whole numpy array. To count elements rows and columns wise we pass the axis as we are in the below programs.

• In the case of the multidimensional array To count the elements in a column We pass argument axis=0 in the count_nonzero() function.
• To count the element row-wise in the case of multidimensional array We pass argument axis=1 in count_nonzero() function.
```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])

print('Count of numpy element :',np.count_nonzero(nparr < 7))

print('Count of numpy element columns-wise :',np.count_nonzero(nparr < 7,axis=0))

print('Count of numpy element rows-wise :',np.count_nonzero(nparr < 7,axis=1))
```

Output

```Count of numpy element : 6
Count of numpy element columns-wise : [2 1 1 1 1]
Count of numpy element rows-wise : [5 1 0]
```

### 3. NumPy count elements by condition with any()

In this python program, we are finding any elements of the numpy array that satisfy the given condition we have used np. any() function along with np. sum() to count a number of element pass conditions.Instead of np.sum() the count_nonzero() to get the fast result.

• To get the count of the number of elements column-wise we have used the axis=0
• To get a count of the number of elements row-wise in numpy array to satisfy given condition used axis=1
```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
print('SIngle elelemt :',np.any(nparr < 14))
print('element column-wise:',np.any(nparr < 14,axis=0))
print('element row-wise :',np.any(nparr < 14,axis=1))

#count the elements
print('any elelemt :',np.sum(np.any(nparr < 14)))
print('Any element column-wise:',np.sum(np.any(nparr < 14,axis=0)))
print('Any element row-wise :',np.sum(np.any(nparr < 14,axis=1)))
```

Output

```any elelemt : True
element column-wise: [ True  True  True  True  True]
element row-wise : [ True  True  True]

any elelemt : 1
Any element column-wise: 5
Any element row-wise : 3
```

### 4. NumPy count elements by condition with where()

In this Python program, we have used the numpy function np.where() to count the number of elements that satisfy the given condition.

```result = np.where(nparr<=6)
element_count = result.size
print('Number of Numpy element count by condition:',element_count)
```

Output

```Number of Numpy element count by condition: 6
```

### 5. NumPy count element by condition using all()

In this Python program example, we have used the numpy function np. all() along with np.count_nonzero() to count the number of elements that satisfy the given condition.

• To get the count of the number of elements column-wise we have used the axis=0
• To get a count of the number of elements row-wise in numpy array to satisfy given condition used axis=1
```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
print(' numpy elelemt count:',np.count_nonzero(np.all(nparr < 14)))
print('numpy element count column-wise:',np.count_nonzero(np.all(nparr < 14,axis=0)))
print('numpy element count row-wise :',np.count_nonzero(np.all(nparr < 14,axis=1)))
```

Output

``` numpy elelemt count: 0
numpy element count column-wise: 3
numpy element count row-wise : 2
```

### 6. NumPy count element by condition all nan value

To count nan values in numpy array the np.isnan() function is used and np.count_nonzero() to count nan values axis=0 for column-wise and axis=1 for row-wise.

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('All nan count in Numpy array:',np.count_nonzero(np.isnan(nparr)))

print('nan count in Numpy array column-wise:',np.count_nonzero(np.isnan(nparr), axis=0))

print('nan count in Numpy array row-wise:',np.count_nonzero(np.isnan(nparr), axis=1))
```

Output

```All nan count in Numpy array: 3
nan count in Numpy array column-wise: [0 1 1 0 1]
nan count in Numpy array row-wise: [0 2 1]
```

### 7. Numpy Count infinite element by condition

To count inf values in the numpy array,First, we will use the np.isinf() function to find inf values and np.count_nonzero() to count inf values in count in each column of the numpy array by using axis=0 and to find inf count in each row of numpy array axis=1

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.inf, 8, 9, np.inf],
[11, 12, np.inf, 14, 15,]])

print('All inf count :',np.count_nonzero(np.isinf(nparr)))

print('inf count column-wise:',np.count_nonzero(np.isinf(nparr), axis=0))

print('inf count row-wise:',np.count_nonzero(np.isinf(nparr), axis=1))
```

Output

```All inf count : 3
inf count column-wise: [0 1 1 0 1]
inf count row-wise: [0 2 1]
```