Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy/pandas element-wise in operator

I have array (or rather pandas frame) that has a column A, values in this columns are integers (let's assume that they belong to range 1..10).

Now I would have to select rows in this array that have A values of {3, 6, 9} (in this example it is possible to just or == operations but in real life this set be a lot longer.

Is there any funciton in either library (pandas or numpy) that allows me to do following fast:

arr = pandas.DataFrame(...)
values = [3, 6, 9] 
valid_indexes = magic_function(arr.A, values)

or in numpy:

arr = np.ndarray(...)
values = [3, 6, 9] 
valid_indexes = magic_function(arr[13, :], values)

In other words I'm looking for element-wise in operator.

like image 472
jb. Avatar asked Aug 13 '14 22:08

jb.


1 Answers

docs are here

arr.loc[arr.A.isin([3,6,9])]
like image 90
Jeff Avatar answered Nov 14 '22 22:11

Jeff