w3resource

NumPy String: numpy.char.count() function

numpy.char.count() function

The numpy.char.count() function returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].

The numpy.char.count() function is useful in text processing and data cleaning tasks, where one needs to count the frequency of a particular word or character in a given dataset. This function can be used to extract information from a dataset by counting the occurrences of a particular keyword.

Syntax:

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

Parameters:

Name Description Required /
Optional
a: array_like of str or unicode Input an array_like of string or unicode. Required
sub: str or unicode The substring to search for. Required
start, end: int Optional arguments start and end are interpreted as slice notation to specify the range in which to count. Optional

Return value:

out : ndarray - Output array of ints.

Example: Counting occurrences of a substring in a numpy string array


>>> c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> c
array(['aAaAaA', '  aA  ', 'abBABba'],
    dtype='|S7')
>>> np.char.count(c, 'A')
array([3, 1, 1])
>>> np.char.count(c, 'aA')
array([3, 1, 0])
>>> np.char.count(c, 'A', start=1, end=4)
array([2, 1, 1])
>>> np.char.count(c, 'A', start=1, end=3)
array([1, 0, 0])

The above code demonstrates the application of the numpy.char.count() function to count the number of occurrences of a substring within a numpy string array. The function is also used to count the number of occurrences of a substring within a specific range of indices in each element of the array.

Pictorial Presentation:

NumPy String operation: numpy.char.count() function

Pictorial Presentation:

NumPy String operation: numpy.char.count() function

Python - NumPy Code Editor:

Previous: zfill()
Next: find()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/string-operations/count.php