Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas find index of subarray in array

I have arrays

a = numpy.array([1,3,5,7])
b = pandas.Series([1,2,3,4,5,6,7,8,9])

Is there a quick command to find all of the matching indices in b that contain a value in a? For example

a in b = [0,2,4,6]
like image 439
Michael Avatar asked Dec 08 '25 09:12

Michael


1 Answers

You can use isin to find where values exist in an array-like object (also works for list):

In [14]:

a = np.array([1,3,5,7])
b = pd.Series([1,2,3,4,5,6,7,8,9])
# call .index if you are just interested in the index values
b[b.isin(a)].index
Out[14]:
Int64Index([0, 2, 4, 6], dtype='int64')

Without accessing the .index attribute you get a series returned:

In [15]:

b[b.isin(a)]
Out[15]:
0    1
2    3
4    5
6    7
dtype: int64
like image 193
EdChum Avatar answered Dec 09 '25 23:12

EdChum