In this Python NumPy tutorial, we will learn** how to count the number of values in NumPy array **Python. With the **Python NumPy count function**, we will cover these topics.

- Python numpy count occurrences
- Python numpy count values
- Python numpy count nonzero
- Python numpy count values in array
- Python numpy count true
- Python numpy count nan
- Python numpy count zeros
- Python numpy count positive
- Python numpy count rows
- Python numpy count where
- Python numpy count element in list
- Python numpy count unique values
- Python numpy count function

## Python NumPy count

- In this section, we will discuss how to count the number of values in the Python NumPy array.
- To do this task we are going to use the
**numpy.char.count()**function. In Python, this function is used to return the count of occurrence of a given string. This method takes four parameters and it is available in the NumPy package module. - In Python, we don’t have the count function in the NumPy package. So we are going to use the actual function that is
**numpy.char.count()**and it will always return a numpy array with the number of non-overlapping occurrences in each word of the list.

**Syntax:**

Let’s have a look at the Syntax and understand the working of Python **numpy.char.count()**

```
char.count
(
a,
sub,
start=0,
end=None
)
```

- It consists of a few parameters
**a:**This parameter indicates the input array or it may be an input string and the datatype is either string or array.**sub:**This is a parameter that represents the substring to search in the input array provided.- start: By default, it takes
**0**value and it is used to set the boundary inside the substring to be searched. The start and end parameters are used individually as per the given demand.

**Example:**

Let’s take an example and check how to count the number of values in the NumPy array

**Source Code:**

```
import numpy as np
new_array = np.array(['yquwwi','wwibgf','asdwwij','wbsbwwikl','wwidvhgqqd'])
print("Creation of array:",new_array)
result =np.char.count(new_array,'wwi')
print("Count numbers:",result)
```

In the above code we import the numpy module and then we have to search the substring without given limits. In this example, there are five strings in the NumPy array and we have to count the **‘wwi’** substring.

Here is the execution of the following given code

Also, check: Python NumPy Minimum tutorial

## Python numpy count occurrences

- In this Program, we will learn how to count the occurrences of elements in the Python NumPy array.
- To perform this particular task we are going to use the
**bincount()**function. In Python**numpy.bincount()**is used to count the number of occurrences of each element inside the numpy array. This method takes three parameters and all the values within the numpy array must be of integer data type. - The length of out is equal to
**np.amax(x)+1**. For example, suppose we have an array that contains integer values**[2,23,45,6,7,8]**then the size of the bin will be 46 because**45**is the maximum number in the array.

**Syntax:**

Let’s have a look at the Syntax and understand the working of Python **numpy.bincount() **function

```
numpy.bincount
(
x,
weights=None,
minlength=0
)
```

- It consists of a few parameters
**x:**This parameter indicates the input array.**weights:**By default it takes none value and it is an additional array of the same shape.**minlength:**It represents the minimum number of bins for the output array.

**Example:**

Let’s take an example and check how to count the occurrence of elements in an array

**Source Code:**

```
import numpy as np
new_array = np.array([24, 15,27,38,15,16,25,38,485,15,16,15])
result = np.bincount(new_array)
count_num= result[15]
print("count occurrence:",count_num)
```

In the above program, we have to count the occurrence of value** ’15’** in a numpy array. By using the **numpy.bincount() **function we can easily return the count occurrence of element **’15’ **in the array. Once you will print **‘count_num’ **then the output will display the **‘4’**.

Here is the implementation of the following given code

Read: Python NumPy Stack with Examples

## Python numpy count values

- Let us see how to count the values in Python numpy array by using the
**collection.counter()**method. - In this example, we will import the collection module for using the
**collection.counter()**function. In Python, this function is used to count each element present in the container-like array and it is the unordered collection and stored values as dictionary keys and count as values. - This method is available in the collection module package and can also use the iterable as a parameter for creating a counter object.

**Source Code:**

```
import numpy as np
import collections
new_values = np.array([16,34,56,16,56,72,16,56,34,72,16])
result=collections.Counter(new_values)
print("Count values in array:",result)
```

In the above program, we have an array that contains integers values, and when you use the** counter()** function in an array, it will count how many times array values repeat is present.

Here is the Screenshot of the following given code

Read: Python NumPy round + 13 Examples

## Python numpy count nonzero

- In this section, we will discuss how to count the nonzero values in Python numpy array.
- In Python count
**nonzero()**means to count the nonzero values present in the given numpy array. To perform this particular task we are going to use the**numpy.count_zero()**function. This method will help the user to find the count number of the elements in the input array which are not zero. - This method is available in the numpy package module and it takes three parameters and always returns the total number of
**non-zero**values in the numpy array.

**Syntax:**

Here is the Syntax of Python** numpy.count_nonzero()** function

```
numpy.count
(
a,
axis=None,
*,
keepdims=False,
)
```

- It consists of a few parameters
**a:**This parameter indicates the input array which we want to count non-zeros values.**axis:**By default it takes none value and it indicates that non-zeros will be counted along with the flattened array.**keepdims:**This is an optional parameter and it represents the boolean value. By default it sets as ‘False’ the axes counted as right.

**Example:**

Let’s take an example and understand the working of **numpy.count_nonzero()** function

**Source Code:**

```
import numpy as np
new_array= np.array([1,2,16,0,15,0,34])
new_output= np.count_nonzero(new_array)
print("Counted non-zero values:",new_output)
```

In the above code, we imported the numpy library and then initialize an array by using the **numpy.array()** function. After that, we have declared a variable **‘new_output’** and then assign a function **np.count_nonzero()**. Once you will print** ‘new_output’** then the output will display the count of non-zero values.

Here is the implementation of the following given code

Read: Python NumPy argsort

## Python numpy count values in array

- In this Program, we will discuss how to count the values in NumPy array Python.
- By using the array condition and
**sum()**method we can easily perform this particular task. Firstly we will create an array by using the**np.array()**function and assign integer values to it. - Now we will declare a variable
**‘z’**and apply the condition if the**’18’**value is available in the array then it will count the values.

**Example:**

```
import numpy as np
array_values = np.array([12,14,15,12,14,17,18,19,21,12,18])
z=(array_values == 18).sum()
print("Count values:",z)
```

Here is the Output of the following given code

Read: Python NumPy repeat

## Python numpy count true

- In this section, we will discuss how to get the true value while counting in NumPy array Python.
- To perform this particular task we are going to use the
**numpy.count_zero()**function. This method will help the user to find the count number of the elements in the input array which are not zero. - In this example, we will consider a nonzero number as
**‘true’**and a zero number as**‘false’**. Once you will print ‘result’ then the output will display the number**‘5’**that indicates how many true values are available in a given array.

**Source Code:**

```
import numpy as np
new_array = np.array([True, False,True, False, False, True, True, True, False])
result = np.count_nonzero(new_array)
print("Count true values:",result)
```

You can refer to the below Screenshot

Read: Python NumPy Data types

## Python numpy count nan

- Let us see how to count the nan values in NumPy array Python by using the np.count_zero() function.
- In this example, we will initialize an array by using the
**numpy.array()**function and contains integer and nan values in it. Now we want to count only nan values from a given array. - To do this task we are going to use the
**np.count_nonzero()**function and assign array as an argument. Once you will print ‘new_result’ it will display the count number of nan values.

**Syntax:**

Here is the Syntax of **np.count_nonzero()** function

```
numpy.count
(
a,
axis=None,
*,
keepdims=False,
)
```

**Example:**

```
import numpy as np
new_values= np.array([np.nan,np.nan,np.nan,0,np.nan,0,np.nan])
new_result= np.count_nonzero(new_values)
print("Counted nan values:",new_result)
```

Here is the execution of the following given code

Read: Python NumPy 2d array

## Python numpy count zeros

- In this section, we will discuss how to count the zeros value in Python NumPy array.
- To do this task we are going to create the array and assign integer values to it. After that we have used the
**np.count_nonzero()**function and set the condition**new_values==0**that indicates when we execute this program then it will check the condition of how many zeros are available in an array.

**Source Code:**

```
import numpy as np
new_values= np.array([0,1,12,0,24,0,8,0])
result= np.count_nonzero(new_values==0)
print("Count zero values:",result)
```

Here is the implementation of the following given code

Read: Python NumPy Split

## Python numpy count positive

- In this section, we will discuss how tp count the positive values in Python numpy array.
- By using the np.sum() method, we can easily solve this problem. In Python, this method is used to generate the sum of all values. In this example, we have just created an array and assigned positive and negative values.
- Now declare a variable
**‘d’**and apply the**np.sum()**function. Within this function, we have assigned the**condition >0**that indicates the count of positive values.

**Example:**

```
import numpy as np
new_array = np.array([[15, 26, 78, 84, -76, 32], [17, 24, 83, -47, -93, 29], [123, 65, 39, 99, -26, 162]])
d= np.sum((new_array) >= 0, axis=0)
print(d)
```

Here is the Screenshot of the following given code

Read: Python NumPy 3d array

## Python numpy count rows

- In this Program, we will discuss how the count the rows in Python NumPy array.
- To do this task we are going to use the
**numpy.shape()**method. In Python, this method is used to check the shape and size of a given array and it will return in the form of tuples of integers. - This method is available in the numpy package module and it will show the length of the numpy array.

**Syntax:**

Here is the Syntax of Python** numpy.shape()**

`numpy.shape(a)`

**Example:**

```
import numpy as np
new_arr = np.array([[12, 25, 67, 98, 13, 98], [34, 89, 94, 134, 245, 987], [256, 456, 945, 678, 912, 876]])
count_row = new_arr.shape
print("Count Rows in array:",count_row)
```

In the above code, we imported the numpy library and then initialize an array by using the **numpy.array()** function. After that, we declare a variable and assign the n**umpy.shape() **function. Once you will print** ‘count_row’** then the output will display counted rows.

Here is the Screenshot of the following given code

Read: Python NumPy Replace + Examples

## Python numpy count where

- In this Program, we will discuss how to use the where condition in numpy
**count_nonzero()**function. - This method will help the user to find the count number of the elements in the input array which are not zero.

**Syntax:**

Here is the Syntax of numpy count_nonzer() function

```
numpy.count
(
a,
axis=None,
*,
keepdims=False,
)
```

**Source Code:**

```
import numpy as np
new_array = np.array([24, 15,27,38,15,16,25,38,485,15,16,15])
print(np.count_nonzero(new_array < 45))
```

Here is the Screenshot of the following given code

Read: Python NumPy Matrix Multiplication

## Python numpy count element in list

- In this section, we will discuss how to count the element in Python list.
- To do this task we are going to use the
**len()**function. In Python the**len()**is a built-in function in Python and it is used to get the length or count of the list. - This method will always return the length of the list and it will help the user to get easily the count of numbers in the Python list.

**Example:**

```
new_list = [45, 67, 98,145, 28]
lis_count = len(new_list)
print("Counted numbers in list:",lis_count)
```

Here is the implementation of the following given code

Now we will see another example of the Python numpy count element in the list

- By using the list
**count()**function we can easily perform this particular task and**count()**is a built-in function in Python. - In Python, this method is used to get the count of a given element in a Python list and this function is used in the list as well as in a string.
- This method takes only one parameter and always returns the count of a given value in the Python list.

**Syntax:**

Here is the Syntax of the** list count()** method

`list.count(element)`

**Example:**

Let’s take a look at the example and understand the working of the list count() method

**Source Code:**

```
lis_ele = [156, 245, 345,896, 28,98,98]
lis_count = lis_ele.count(98)
print("Counted numbers in list:",lis_count)
```

In the following given code, we created a list that contains only the integer values. Next, we declared a variable **‘lis_count’** and assign the** list.count()** function.

You can refer to the below Screenshot

Read: Python NumPy Add Tutorial

## Python numpy count unique values

- In this section, we will discuss how to count the unique values in Python.
- To do this task we are going to apply the concept of
**numpy.unique()**function. To get the detail information about numpy unique function. You can refer our Python numpy unique article.

**Source Code:**

```
import numpy as np
new_array = [16,16,17,16,27,27,18,17,18]
result=np.array(np.unique(new_array, return_counts=True)).T
print("Count unique values:",result)
```

In the following code, we imported the numpy library and then initialize an array by using the **np.array()** function. Next, we have used the **np.unique() **function and pass return_counts as an argument. Once you will print **‘result’** then the output displays the counted unique values number.

Here is the execution of the following given code

Read: Python NumPy Indexing

## Python numpy count function

- In this program, we will learn how to use the
**count()**function in Python numpy array. - By using the
**np.char.count()**function we can solve this problem and this function will always return a numpy array with the number of non-overlapping the occurrences in each word of the list.

**Syntax:**

Here is the Syntax of **numpy.char.count()** function

```
char.count
(
a,
sub,
start=0,
end=None
)
```

**Example:**

```
import numpy as np
new_values = np.array(['16723','787224','8972876','244523'])
print("Creation of array:",new_values)
new_output =np.char.count(new_values,'72')
print("Count numbers:",new_output)
```

In the above code, we imported the numpy library and then created a NumPy array by using the **np.array()** function. Next, we have used the np.char.count() function and in this example, there are four strings in the NumPy array and we have to count the **’72’ **substring.

Here is the Output of the following given code

Also, take a look at some more tutorials on Python NumPy.

- Python NumPy Filter + 10 Examples
- Python Copy NumPy Array
- Python NumPy Delete – Complete Tutorial
- Python NumPy Normalize + Examples
- Python NumPy Savetxt + Examples
- Python NumPy Median + Examples
- Python NumPy genfromtxt() – Complete Tutorial
- How to convert numpy array to list of strings in Python

In this Python NumPy tutorial, we have learned** how to count the number of values in NumPy array **Python. Moreover, we have also covered these topics.

- Python numpy count occurrences
- Python numpy count values
- Python numpy count nonzero
- Python numpy count values in array
- Python numpy count true
- Python numpy count nan
- Python numpy count zeros
- Python numpy count positive
- Python numpy count rows
- Python numpy count where
- Python numpy count element in list
- Python numpy count unique values
- Python numpy count function

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.