1

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.

1
  • Is xyzv a numpy 2d array ?wouldn't this xyzv[:,3].astype(int) == 100 give a boolean value ? And how could you pass a boolean value in here xyzv[boolean] ? Commented Jul 13, 2015 at 12:55

1 Answer 1

1

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.]])
Sign up to request clarification or add additional context in comments.

1 Comment

Just small amendment, I asked to delete which is not in search_nums. Which in other words means keep what is in search_nums. So I deleted ~ to not invert it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.