AppDividend
Latest Code Tutorials

np.count: How to Count Number of Elements in Python Array

The np.char.count() is a numpy library function that counts the total number of occurrences of a character in a string or the array. It comes under the char class. If the np.char.count() function is used in an array, it returns an array containing a count of the character in each array element.

Syntax

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

Arguments

The np.char.count() function takes two required arguments as a parameter and has two optional arguments:

  1. arr: It is an array or string passed in this argument. This array is the required argument for returning the count. The array in which the count is needed to be found is passed in this argument.
  2. sub: The substring is given in this argument. This substring needs to be counted in the array. This substring is searched in the array. If the match is found, then the count is increased. This is the required argument.
  3. start: It is the starting index of the array. It is an optional argument. If this argument is passed with some values, then the count function is applied from the starting index of each element in the array. The count function is not applied to the indices before the starting index of each element in the array.
  4. end: It is the ending index of the array. It is an optional argument. If this argument is passed with some values, then the count function is applied till the ending index specified in the argument.

Return value

It returns an array.  This array consists of counting the number of times the substring is present in the array. If the substring is present in the element, then the count is incremented by one.

Python program for returning the count of the number of occurrences of the substring in the array using np.char.count()

# Importing numpy as np
import numpy as np

# Creating a numpy array called arr
arr = np.array(['Hello How are You', 'an apple for a day', 'aaasskdfj', 'He is aakash'])
print("The array is : ", arr)
count = np.char.count(arr, 'a')
print("Count of 'a' in array", count)

Output

The array is : ['Hello How are You' 'an apple for a day' 'aaasskdfj' 'He is aakash']
Count of 'a' in array [1 4 3 3]

We imported a numpy package consisting of functions for numerical calculations in this program. Then, we created a numpy array called arr using the function called np.array(). This array consists of several strings inside the numpy array.

In the next step, we passed this array to the np.char.count() function that presents in the char class. The np.char.count() function counts the number of occurrences of the substring in the elements present in the string. It first takes the first element and finds the count and stores this in the first index of the output array.

Similarly, the count is found for all the other elements in the array. Hence the count for all the elements is found and returned as an array.

Python program for returning the count of the number of occurrences of the substring in the array using np.char.count() 

# Importing numpy as np
import numpy as np

# Creating a numpy array called arr
arr = np.array(['Hello How are You', 'an apple for a day','aaasskdfjdgfgs', 'He is aakash'])
print("The array is : ", arr)
count = np.char.count(arr, 'a', start=3, end=10)
print("Count of 'a' in array", count)

Output

The array is : ['Hello How are You' 'an apple for a day' 'aaasskdfjdgfgs' 'He is aakash']
Count of 'a' in array [0 1 0 3]

In this program, we passed the values for the start and end index. This finds the count of a number of occurrences of the substring in between starting and ending index.

That’s it for np.count() function tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.