Numpy: Drop rows with all nan or 0 values

前端 未结 5 1364
梦毁少年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:45

    In addition: if you want to drop rows if a row has a nan or 0 in any single value

    a = np.array([
        [1, 0, 0],
        [1, 2, np.nan],
        [np.nan, np.nan, np.nan],
        [2, 3, 4]
    ])
    
    mask = np.any(np.isnan(a) | np.equal(a, 0), axis=1)
    a[~mask]
    

    Output

    array([[ 2.,  3.,  4.]])
    

提交回复
热议问题