Numpy Count | Practical Explanation of Occurrence Finder

Numpy one of the best and most widely used modules.Because it makes the computation easy and simple with faster speed. Similarly, we have a numpy count, a method to find a substring occurrence in a given array or list. This is easy to use, and simple is working. But like Numpy, the behind the scenes things are complex.

What is Numpy Count?

The actual function is num.char.count(). So the syntax changes a little. But function work remains the same: to find the count of occurrence of a particular string. That, too, from a given list of words without overlapping the occurrence in each word of the list.

Syntax of Numpy Count

The following is the syntax of using numpy count function:

numpy.core.defchararray.count(arr, substring, start=0, end=None)

While the usual way of writing it is :

np.char.count(array, substring)

Parameters of Numpy Count

ParameterMandatory or Not

arr : array

arr or (array) is the base of our function. Because this parameter provides the list of strings. While it is a ndarray, it is easily traversed by function. And the count is done for each string separately. The data type used is either array-like or string only.


substring tells the combination of the characters to be searched in the array provided. That is to say, it is mandatory to be used. The function considers all the characters as one. So it becomes easier to traverse and count. The data type used should be str or Unicode.

start / end

They define the boundaries of search. Because they are the positions of search in a string. They are optional to use and usually not used. They have an int data type. Start and end both are used individually as per the demand. While the position by the end is not included, the start is included. The limits they make are for each string irrespective of others.

Return Value

As per the, This function returns an array. That contains the number of non-overlapping occurrences of substring sub in the range [start, end]. In other words, An integer array with the number of non-overlapping occurrences of the substring.

Examples of Numpy Count

Before we start coding we need to import the module – numpy:

import numpy as np

Now we are ready to code. So we create an array:

arr = np.array(['ahhert','bhherta','hhertab','jhahhertb'])
array(['ahhert', 'bahherta', 'hhertab', 'jhahhertb'], dtype='<U9')

Now we use the function on it:

array([1, 1, 1, 1])
array([0, 0, 1, 0])

Here, we searched two different substrings without defining limits. And the result shows the working. There are four strings and in each, we have ‘hhe’ only once. While there were more ‘h’ it only considered the combined ones. The same goes for the second example, ‘ab’ occur together twice. But only once is ‘b’ after ‘a’.

array([1, 2, 0, 2])
array([0, 0, 1, 1])
array([1, 0, 0, 1])
array([0, 0, 1, 0])
array([0, 1, 0, 2])

Here, you can see the start and end can be used individually. And even without using the word start and end, we can assign positions.

Str.count vs Numpy Count

The count is used in one more way in basic python as string.count(). As per W3schools, The method returns the number of times a specified value appears in the string. That is similar to numpy count but a little different.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'count'
a = ' i love to play football'

If we use count directly it gives a traceback error. Because ‘arr’ is an N-dimensional array with multiple string array. While if we use a simple string like ‘a’, it gives similar results. So in numpy count function is called for each string separately and rest is same


When you search for numpy count, you may get this function as well. This Counts the number of non-zero values in the array a. With the syntax: numpy.count_nonzero(aaxis=None*keepdims=False)

It counts the number of nonzero values in an N-dimensional array. Without an axis, it tells for the whole array. With axis, it tells results as a row or column.

Read more >> 4 Examples to Use Numpy count_nonzero() Function

What’s Next?

NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.


In conclusion, we can say that numpy count is commonly used and easily understood. So everyone can use it with comfort and make the computation easier.

Still have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Notify of
Inline Feedbacks
View all comments