removing pairs of elements from numpy arrays that are NaN (or another value) in Python

Posted by user248237 on Stack Overflow See other posts from Stack Overflow or by user248237
Published on 2010-04-23T00:49:58Z Indexed on 2010/04/23 0:53 UTC
Read the original article Hit count: 429

Filed under:
|
|
|

I have an array with two columns in numpy. For example:

a = array([[1, 5, nan, 6],
           [10, 6, 6, nan]])
a = transpose(a)

I want to efficiently iterate through the two columns, a[:, 0] and a[:, 1] and remove any pairs that meet a certain condition, in this case if they are NaN. The obvious way I can think of is:

new_a = []
for val1, val2 in a:
  if val2 == nan or val2 == nan:
    new_a.append([val1, val2])

But that seems clunky. What's the pythonic numpy way of doing this?

thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy