0

Suppose I have three lists where one contains NaN's (I think they're 'NaNs', they get printed as '--' from a previous masked array operation):

a = [1,2,3,4,5]
b = [6,7,--,9,--]
c = [6,7,8,9,10]

I'd like to perform an operation that iterates through b, and deletes the indexes from all lists where b[i]=NaN. I'm thinking something like this:

for i in range(0,len(b):
    if b[i] = NaN:
        del.a[i] etc

b is generated from from masking c under some condition earlier on in my code, something like this:

b = np.ma.MaskedArray(c, condition)

Thanks!

rh1990
  • 880
  • 7
  • 17
  • 32
  • You have some syntax errors there: e.g `if b[i] = Nan`. It should have double ==. Other than that, if you want to remove the NaN values you can do what's written here: http://stackoverflow.com/a/11620982/966922 – javidgon Dec 13 '16 at 09:23
  • Possible duplicate of [Removing nan values from an array](http://stackoverflow.com/questions/11620914/removing-nan-values-from-an-array) – iFlo Dec 13 '16 at 09:26

1 Answers1

1

This is easy to do using numpy:

import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([6,7,np.NaN,9,np.NaN])
c = np.array([6,7,8,9,10])

where_are_nans = np.isnan(b)
filtered_array = a[~where_are_nans] #note the ~ negation
print(filtered_array)

And as you can easily see it returns:

[1 2 4]

Dominik Stańczak
  • 2,046
  • 15
  • 27