NumPy strings.count()

The numpy.strings.count() function counts the number of non-overlapping occurrences of a substring within string arrays. It allows specifying a search range within the string.

Syntax

</>
Copy
numpy.strings.count(a, sub, start=0, end=None)

Parameters

ParameterTypeDescription
aarray_like (StringDType, bytes_, or str_ dtype)Array of strings where the substring will be searched.
subarray_like (StringDType, bytes_, or str_ dtype)The substring to count occurrences of.
startarray_like (integer dtype), optionalStarting index for substring search (default is 0).
endarray_like (integer dtype), optionalEnding index for substring search (default is full string length).

Return Value

Returns an array of integers indicating the count of the specified substring in each string element.


Examples

1. Counting a Substring in an Array of Strings

In this example, we count how many times the letter 'a' appears in an array of fruit names.

</>
Copy
import numpy as np

# Define an array of fruit names
fruits = np.array(['apple', 'banana', 'cherry', 'grape'])

# Count occurrences of 'a' in each string
count_a = np.char.count(fruits, 'a')

# Print results
print("Fruits:", fruits)
print("Occurrences of 'a':", count_a)

Output:

Fruits: ['apple' 'banana' 'cherry' 'grape']
Occurrences of 'a': [0 3 0 1]

2. Counting a Multi-Character Substring

We now count how many times the substring 'an' appears in each fruit name.

</>
Copy
import numpy as np

# Define an array of fruit names
fruits = np.array(['apple', 'banana', 'cherry', 'grape'])

# Count occurrences of 'an'
count_an = np.char.count(fruits, 'an')

# Print results
print("Fruits:", fruits)
print("Occurrences of 'an':", count_an)

Output:

Fruits: ['apple' 'banana' 'cherry' 'grape']
Occurrences of 'an': [0 2 0 0]

3. Using the start and end Parameters

We restrict the search range to specific portions of each string using the start and end parameters.

</>
Copy
import numpy as np

# Define an array of fruit names
fruits = np.array(['apple', 'banana', 'cherry', 'grape'])

# Count occurrences of 'a' only in the first 3 characters of each string
count_limited = np.char.count(fruits, 'a', start=0, end=3)

# Print results
print("Fruits:", fruits)
print("Occurrences of 'a' (first 3 characters only):", count_limited)

Output:

Fruits: ['apple' 'banana' 'cherry' 'grape']
Occurrences of 'a' (first 3 characters only): [0 1 0 1]

In this case, only the first three characters of each string are checked for occurrences of 'a'.

4. Counting Substrings in a Mixed String Array

This example demonstrates how np.char.count() works with a variety of words in an array.

</>
Copy
import numpy as np

# Define an array of words
words = np.array(['strawberry', 'blueberry', 'raspberry', 'blackberry'])

# Count occurrences of 'berry'
count_berry = np.char.count(words, 'berry')

# Print results
print("Words:", words)
print("Occurrences of 'berry':", count_berry)

Output:

Words: ['strawberry' 'blueberry' 'raspberry' 'blackberry']
Occurrences of 'berry': [1 1 1 1]

Since all words contain the substring 'berry' exactly once, the output returns 1 for each element.