Skip to content Skip to sidebar Skip to footer

Delete Nan And Corresponding Elements In Two Same-length Array

I have two lists of the same length that I can convert into array to play with the numpy.stats.pearsonr method. Now, some of the elements of these lists are nan, and can thus not b

Solution 1:

The accepted answer to proposed duplicate gets you half-way there. Since you're using Numpy already you should make these into numpy arrays. Then you should generate an indexing expression, and then use it to index these 2 arrays. Here indices will be a new array of bool of same shape where each element is True iff not (respective element in x is nan or respective element in y is nan):

>>>x
array([  1.,   2.,  nan,   4.,   5.,   6.])
>>>y
array([  1.,  nan,   3.,  nan,   5.,   6.])
>>>indices = np.logical_not(np.logical_or(np.isnan(x), np.isnan(y)))>>>x = x[indices]>>>y = y[indices]>>>x
array([ 1.,  5.,  6.])
>>>y
array([ 1.,  5.,  6.])

Notably, this works for any 2 arrays of same shape.

P.S., if you know that the element type in the operand arrays is boolean, as is the case for arrays returned from isnan here, you can use ~ instead of logical_not and | instead of logical_or: indices = ~(np.isnan(x) | np.isnan(y))

Post a Comment for "Delete Nan And Corresponding Elements In Two Same-length Array"