np.count() function in Python [4 Examples]

Do you know how the NumPy count() function works? In this NumPy blog, I will explain what the np.count() function in Python is, its syntax, parameters required, and its return values with some illustrative examples.

The np.count() function in Python is a tool used for counting occurrences of a specific substring within each element of an array. Part of the NumPy library, this function works efficiently on arrays, including multi-dimensional ones, to find and count instances of a given word or character.

np.count() function in Python

The np.char.count() function in Python is part of the NumPy library, which is widely used for numerical computations. This function is specifically used for performing vectorized string operations for arrays of dtype numpy.str_ or numpy.unicode_.

It counts the number of occurrences of a substring in each element of an array.

np.count() Function Syntax

The np.count() function in Python syntax is as follows:

np.char.count(arr, sub, start=0, end=None)

NumPy count() Function Parameter

Here,

arrThis is the array in which the function searches for the substring in Python.
subThe substring to search for within each element of arr in Python.
startThe starting position in each element of arr in Python from which the search begins. The default is 0.
endThe ending position in each element of arr in Python up to which the search is performed. None means the search continues until the end of each element. The default is None.
List of the parameters required in the np.count() function in Python.

NumPy count() function in Python return values

The np.count() function in Python returns an array of integers, with each element representing the count of the substring sub in the corresponding element of the input array arr.

READ:  Python Tkinter Filter Function() - How to use

numpy.count() function in Python use cases

Let’s see some examples where we can learn the use of the np.count() function in Python.

1. NumPy count occurrences of all values in a Python array

In this example, the np.count() function in Python counts occurrences of the substring ‘hello’ in each element of the array arr.

import numpy as np

arr = np.array(['apple pie', 'baseball game', 'American dream', 'statue of liberty'])
sub = 'American'
result = np.char.count(arr, sub)
print(result)

Output:

[0 0 1 0]

The output from running the code in PyCharm is visually represented in the screenshot below.

np.count() function in Python

2. np.count in Python with start and end parameter

Here, the np.count() function in Python searches for the substring between two positions in each element of the array.

import numpy as np

arr = np.array(['Alabama', 'Alaska', 'California', 'Arizona'])
sub = 'a'
result = np.char.count(arr, sub, start=1, end=4)
print(result)

Output:

[1 1 1 0]

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

np count function in python

3. numpy.count() function in 2D array

Here, we have to count the occurrence of the value in a 2D array in Python.

import numpy as np

arr = np.array([['freedom of speech', 'freedom of expression'],
                ['freedom fighters', 'land of the free']])
sub = 'freedom'
result = np.char.count(arr, sub)
print(result)

Output:

[[1 1]
 [1 0]]

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

python numpy count in 2D array

4. NumPy count occurrences of values in a 2D array with start and end parameters

let’s incorporate the start and end parameters in examples with 2D arrays using the np.char.count() function.

import numpy as np

arr = np.array([['liberty and justice', 'pursuit of liberty'],
                ['statue of liberty', 'liberty bell']])

sub = 'liberty'
start, end = 0, 10
result = np.char.count(arr, sub, start=start, end=end)
print(result)

Output:

[[1 0]
 [0 1]]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

python numpy count values

Conclusion

In this article, I have explained what the np.count() function in Python is in detail, how we can apply it, what the parameters required, and the return value. Also, different use cases in Python illustrate the np.char.count() function.

READ:  Python Scipy Minimize [With 8 Examples]

You may also like to read: