I would like to use the numpy.where function in an array of strings. However, I was not successful. Can someone please help me figure this out?
For example, when I use numpy.where in the following example, I get an error:
import numpy as np A = ['apple', 'orange', 'apple', 'banana'] arr_index = np.where(A == 'apple',1,0)
I get the following:
>>> arr_index array(0) >>> print A[arr_index] >>> apple
However, I would like to know the indices in the array of strings, A , where the string 'apple' matches. In the line above, this happens at 0 and 2. However, np.where returns 0, not 2.
So how do I get numpy.where to work with strings? Thank you in advance.
Rohit source share