1

I am coming from IDL and trying to find and eliminate the NaNs in two lists of data. lets say there is a NaN in position 5 for list A but not list B. I need position 5 to be removed in both lists. Like so...

A = [1, NaN, 3, 4, NaN, 6, 7, 8, 9]

B = [1', 2', 3', NaN, 5', 6', 7', 8', NaN]

A_new = [1 , 3 , 6 , 7 , 8 ]

B_new = [1', 3', 6', 7', 8']

Here is the IDL code that works fine. I just need it translated to python and I am stumped.

;Removes the NANs

loc = where((Finite(pdcsapflux) EQ 1)and(Finite(time) EQ 1))

flux_nonan = pdcsapflux(loc)

time_nonan = time(loc)

Thanks!!

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Thomas Cannon
  • 89
  • 2
  • 12

4 Answers4

1
A = [1, None, 3, 4, None, 6, 7, 8, 9]
B = [1, 2, 3, None, 5, 6, 7, 8, None]
print zip(*[
    (a, b) for a, b in zip(A, B)
    if a is not None and b is not None
])

Documentation of zip

satoru
  • 31,822
  • 31
  • 91
  • 141
1

Following on @DSM's suggestion, if you're coming from IDL, you probably want to use numpy with actual arrays, not lists. The direct replacement for the IDL code using numpy is more like the following:

import numpy.random as ran
import numpy as np
arr = ran.rand(10)  # create some fake data
arr[[1,3,5]] = np.nan  # add some "bad" values
arr2 = arr[np.where(~np.isnan(arr))]

Hope this helps.

Chris Torrence
  • 452
  • 3
  • 11
0

I dont know if python has NaN, just assume that is None.

ta, tb = [], []
for i in range(min(len(a), len(b))):
    if a[i] is None or b[i] is None:
        continue
    ta.append(a[i])
    tb.append(b[i])

ta, tb is your output of a,b at last you should append the rest item of the longer list.

amow
  • 2,203
  • 11
  • 19
0

If you are using numpy you can also do it without where function (usually very slow if you work with big matrices)

import numpy as np 
A = np.array([1, NaN, 3, 4, NaN, 6, 7, 8, 9])
B = np.array([1, 2, 3, NaN, 5, 6, 7, 8, NaN])
A_new = A[~np.isnan(A)]
B_new = B[~np.isnan(B)]
VictorCB
  • 70
  • 10