Numpy: Drop rows with all nan or 0 values

前端 未结 5 1359
梦毁少年i
梦毁少年i 2020-12-29 06:41

I\'d like to drop all values from a table if the rows = nan or 0.

I know there\'s a way to do this using pandas i.e pandas.dropna(how

5条回答
  •  太阳男子
    2020-12-29 07:28

    List comprehension can be used as a one liner.

    >> a = array([65.36512 , 39.98848 , 28.25152 , 37.39968 , 59.32288 , 40.85184 ,
           71.98208 , 41.7152  , 33.71776 , 38.5504  , 21.34656 , 37.97504 ,
           57.5968  , 30.494656, 80.03776 , 33.94688 , 37.45792 , 27.617664,
           15.59296 , 27.329984, 45.2256  , 61.27872 , 57.8848  , 87.4592  ,
           34.29312 , 85.15776 , 46.37696 , 79.11616 ,       nan,       nan])
    
    >> np.array([i for i in a if np.isnan(i)==False])
    
    array([65.36512 , 39.98848 , 28.25152 , 37.39968 , 59.32288 , 40.85184 ,
           71.98208 , 41.7152  , 33.71776 , 38.5504  , 21.34656 , 37.97504 ,
           57.5968  , 30.494656, 80.03776 , 33.94688 , 37.45792 , 27.617664,
           15.59296 , 27.329984, 45.2256  , 61.27872 , 57.8848  , 87.4592  ,
           34.29312 , 85.15776 , 46.37696 , 79.11616 ])
    

提交回复
热议问题