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

前端 未结 7 1835
你的背包
你的背包 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:08

    I came across the problem and had to find a custom solution for scattered NaNs. The function below replaces any NaN by the first number occurrence to the right, if none exists, it replaces it by the first number occurrence to the left. Further manipulation can be done to replace it with the mean of boundary occurrences.

    import numpy as np
    
    Data = np.array([np.nan,1.3,np.nan,1.4,np.nan,np.nan])
    
    nansIndx = np.where(np.isnan(Data))[0]
    isanIndx = np.where(~np.isnan(Data))[0]
    for nan in nansIndx:
        replacementCandidates = np.where(isanIndx>nan)[0]
        if replacementCandidates.size != 0:
            replacement = Data[isanIndx[replacementCandidates[0]]]
        else:
            replacement = Data[isanIndx[np.where(isanIndx

    Result is:

    >>> Data
    array([ 1.3,  1.3,  1.4,  1.4,  1.4,  1.4])
    

提交回复
热议问题