In this article, we will discuss how to count all values in a 2D numpy Array or Matrix in python, which satisfy a condition like greater than a given value etc.

**Table of Contents**

- Count all values greater than a value in 2D Numpy Array.
- Count all values greater than a value in each row of 2D Numpy Array.
- Count all values greater than a value in each column of 2D Numpy Array.

**A background of problem in hand**

Suppose we have a 2D Numpy array,

# Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]])

We want to count all the values in this numpy array which are greater than 3. To do that we can apply a condition on array like,

**Advertisements**

arr > 3

It will convert the 2D number array to a 2D bool array with True at places where condition returns True i.e. value is greater than 3. Bool array will be like,

[[False False False True] [ True True True True] [False False False True]]

Now if we count the number of True elements in this bool array, then it will give us the count of values which are greater than 3. To count True

elements we can use a function count_nonzero().

**numpy.count_nonzero()**

In Python, the 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 complete array. But in case you are dealing with

multi-dimensional array, then you can use the axis argument to count occurrences along the given axis. Let’s use this function to count values in a numpy array that satisfy a condition.

## Count all values greater than a value in 2D Numpy Array in python

Count all the values greater than 3 in complete 2D Numpy array,

import numpy as np # Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]]) # Count of all values greater than 3 in 2D Numpy array count = np.count_nonzero(arr > 3) print('Total values greater than 3 are: ', count)

Output:

Total values greater than 3 are: 6

As we didn’t provided the axis parameter, so ** count_nonzero()** returned all the values which are greater than 3 in complete 2D numpy array.

## Count all the values greater than a value in each row of 2D Numpy array in python

We have a 2D Numpy array and we will count all the values greater than 3 in each row of the 2D Numpy array,

import numpy as np # Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]]) # Count of all values greater than 3 in each row of 2D Numpy Array count = np.count_nonzero(arr > 3, axis=1) print('Count of all values greater than 3 in each row: ', count)

Output:

Count of all values greater than 3 in each row: [1 4 1]

As we provided the axis parameter as 1, so ** count_nonzero()** returned an array containing all the values which are greater than 3 in each row of the 2D numpy array.

## Count all the values greater than a value in each column of 2D Numpy array in python

We have a 2D Numpy array and we will count all the values greater than 3 in each column of the 2D Numpy array,

import numpy as np # Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]]) # Count of all values greater than 3 in each column of 2D Numpy Array count = np.count_nonzero(arr > 3, axis=0) print('Count of all values greater than 3 in each column: ', count)

Output:

Count of all values greater than 3 in each column: [1 1 1 3]

As we provided the axis parameter as 0, so ** count_nonzero() **returned an array containing all the values which are greater than 3 in each column of the 2D numpy array.

**Summary:**

In this article we learned how to count values greater than a vaue in 2D Numpy Array.

**Advertisements**

Thanks for reading.