Numpy 'where' on string

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.

+4
source share
1 answer
 print a[arr_index] 

not array_index !!

 a = np.array(['apple', 'orange', 'apple', 'banana']) arr_index = np.where(a == 'apple') print arr_index print a[arr_index] 
+4
source

Source: https://habr.com/ru/post/1481688/


All Articles