Replace NaN's in NumPy array with closest non-NaN value

前端 未结 7 1730
你的背包
你的背包 2021-02-05 04:22

I have a NumPy array a like the following:

>>> str(a)
\'[        nan         nan         nan  1.44955726  1.44628034  1.44409573\\n  1.4408         


        
7条回答
  •  佛祖请我去吃肉
    2021-02-05 05:19

    I got something like this

    i = [i for i in range(len(a)) if not np.isnan(a[i])]
    a = [a[i[0]] if x < i[0] else (a[i[-1]] if x > i[-1] else a[x]) for x in range(len(a))]
    

    It's a bit clunky though given it's split up in two lines with nested inline if's in one of them.

提交回复
热议问题