Q:

Write a NumPy program to count a given word in each row of a given array of string values

0

Write a NumPy program to count a given word in each row of a given array of string values.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np 

str1 = np.array([['Python','NumPy','Exercises'],
                 ['Python','Pandas','Exercises'],
                 ['Python','Machine learning','Python']])
print("Original array of string values:") 
print(str1)
print("\nCount 'Python' row wise in the above array of string values:")
print(np.char.count(str1, 'Python'))

Sample Output:

Original array of string values:
[['Python' 'NumPy' 'Exercises']
 ['Python' 'Pandas' 'Exercises']
 ['Python' 'Machine learning' 'Python']]

Count 'Python' row wise in the above array of string values:
[[1 0 0]
 [1 0 0]
 [1 0 1]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now