python - How to delete rows where specific column value is not within other array? -


how can delete rows in numpy 2d array value in specific column not within other array/list. speed important.

i found out how keep rows conditioning column specific value:

xyzv = xyzv[xyzv[:,3].astype(int) == 100] 

but need compare way more values (array of values). shoot dark:

xyzv = xyzv[xyzv[:,3].astype(int) in my_values] 

and other variations. python didn't laugh.

you can use np.in1d -

search_nums = [100,102] # list of search numbers. numpy arrays work too. xyzv = xyzv[np.in1d(xyzv[:,3].astype(int),search_nums)] 

sample run -

in [42]: xyzv = np.random.randint(98,104,(7,4)).astype(float)  in [43]: xyzv out[43]:  array([[  98.,  100.,  102.,  102.],        [  99.,  102.,  102.,  101.],        [ 100.,   99.,   98.,  100.],        [ 100.,  101.,  102.,   99.],        [  99.,  102.,  103.,  101.],        [ 103.,  100.,   98.,  102.],        [ 102.,  101.,  103.,  101.]])  in [44]: search_nums = [100,102]  in [45]: xyzv[np.in1d(xyzv[:,3].astype(int),search_nums)] out[45]:  array([[  98.,  100.,  102.,  102.],        [ 100.,   99.,   98.,  100.],        [ 103.,  100.,   98.,  102.]]) 

Comments