Numpy: Drop rows with all nan or 0 values

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

    This will remove all rows which are all zeros, or all nans:

    mask = np.all(np.isnan(arr), axis=1) | np.all(arr == 0, axis=1)
    arr = arr[~mask]
    

    And this will remove all rows which are all either zeros or nans:

    mask = np.all(np.isnan(arr) | arr == 0, axis=1)
    arr = arr[~mask]
    

提交回复
热议问题