Miro
Miro

Reputation: 609

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

How can I delete rows in numpy 2d array where value in specific column is not within other array/list. Speed is important.

I found out how to keep only rows by conditioning column to specific value:

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

But I need to compare it with way more values (array of my values). I just shoot into the dark:

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

And some other variations. But python didn't laugh.

Upvotes: 1

Views: 139

Answers (1)

Divakar
Divakar

Reputation: 221524

You can use np.in1d -

search_nums = [100,102] # List of search numbers. NumPy arrays would 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.]])

Upvotes: 1

Related Questions