Numpy / pandas by elements in statement

I have an array (or rather pandas) that has a column A

, the values ​​in those columns are integers (let's say they belong to the range 1..10).

Now I will need to select rows in this array with values A

{3, 6, 9}

(in this example, you can perform operations simply or ==

, but in real life this set will be much longer.

Is there any funciton in any library ( pandas

or numpy

) that allows me to do the following quickly:

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 am looking for a rudimentary operator in

.

+3


source to share


2 answers


docs here



arr.loc[arr.A.isin([3,6,9])]

      

+5


source


From NumPy, you can use the function numpy.in1d

:

import numpy as np
arr = np.array([5, 10, 13, 7, 2, 2, 4, 18, 9, 3, 1], dtype=np.int32)
values = np.array([10, 2, 9])
valid_indexes = np.in1d(arr, values)

      



http://docs.scipy.org/doc/numpy/reference/generated/numpy.in1d.html

+4


source







All Articles