13

I would like to use the numpy.where function on a string array. However, I am unsuccessful in doing so. Can someone please help me figure this out?

For example, when I use numpy.where on 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 string array, A where the string 'apple' matches. In the above string this happens at 0 and 2. However, the np.where only returns 0 and not 2.

So, how do I make numpy.where work on strings? Thanks in advance.

4 Answers 4

14
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])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the superfast reply! Ah, that is correct. Yes, but what I want are the indices where the string 'apple' matches in the string array. So, I want something like: arr_index = [0,2] # that is 'apple' occurs are positions 0 and 2 in the string array a. However, it only gives me 0.
I just corrected the error in my post so that the main question is highlighted and not the stupid error. :-)
1

I believe an easier way is to just do :

A = np.array(['apple', 'orange', 'apple', 'banana'])
arr_index = np.where(A == 'apple')
print(arr_index)

And you get:

(array([0, 2]),)

Comments

0

the thing is that you need to use an array instead of a list to use where properly (also, use True and False instead of 1 and 0 to get a mask to look for the indexes):

A = ['apple', 'orange', 'apple', 'banana']
arr_mask = np.where(np.array(A) == 'apple',True,False)
arr_index = np.arange(0, len(A))[arr_mask]

This way, you'll get arr_index as: np.array([0,2])

Notice that to use the mask arr_mask or the indexes arr_index to look for the values in A, A needs to be an array:

In [55]: A = ['apple', 'orange', 'apple', 'banana'] 
    ...: arr_mask = np.where(np.array(A) == 'apple',True,False) 
    ...: arr_index = np.arange(0, len(A))[arr_mask]                                                             

In [56]: A[arr_mask]                                                                                            
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-f8b153319425> in <module>
----> 1 A[arr_mask]

TypeError: only integer scalar arrays can be converted to a scalar index

In [57]: A[arr_index]                                                                                           
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-57-91c260fe71ab> in <module>
----> 1 A[arr_index]

TypeError: only integer scalar arrays can be converted to a scalar index

In [58]: B = np.array(A)                                                                                        

In [59]: B[arr_mask]                                                                                            
Out[59]: array(['apple', 'apple'], dtype='<U6')

In [60]: B[arr_index]                                                                                           
Out[60]: array(['apple', 'apple'], dtype='<U6')

What you are getting using just the list is that the function np.where() doesn't find anywhere where the condition is satisfied. If you try:

A = ['apple', 'orange', 'apple', 'banana']
arr_index = np.where(A == 'orange',1,0)

You'll get again array(0) as the output.

Comments

0

Yet another way:

def GetIndexOfStr(npArray,theStr): 
    #npArray is from type of numpy.ndarray where each item is of type np.str
    return np.where(npArray == theStr)[0][0]

A = np.array(['apple', 'orange', 'apple', 'banana'])
print(A[GetIndexOfStr(A,"apple")]) # ==> this will result in "apple"
print(A[GetIndexOfStr(A,"appleX")]) # ==> this will throw IndexError

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.