Python Numpy:删除所有 nan 或 0 值的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22032668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numpy: Drop rows with all nan or 0 values
提问by Black
I'd like to drop all values from a table if the rows = nanor 0.
如果行 =nan或0.
I know there's a way to do this using pandas i.e pandas.dropna(how = 'all')but I'd like a numpy method to remove rows with all nanor 0.
我知道有一种方法可以使用 pandas ie 来做到这一点,pandas.dropna(how = 'all')但我想要一种 numpy 方法来删除带有 allnan或0.
Is there an efficient implementation of this?
有没有有效的实施方式?
采纳答案by HYRY
import numpy as np
a = np.array([
[1, 0, 0],
[0, np.nan, 0],
[0, 0, 0],
[np.nan, np.nan, np.nan],
[2, 3, 4]
])
mask = np.all(np.isnan(a) | np.equal(a, 0), axis=1)
a[~mask]
回答by Jaime
This will remove all rows which are all zeros, or all nans:
这将删除所有全为零或全为 nan 的行:
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:
这将删除所有为零或 nans 的所有行:
mask = np.all(np.isnan(arr) | arr == 0, axis=1)
arr = arr[~mask]
回答by JaminSore
I like this approach
我喜欢这种方法
import numpy as np
arr = np.array([[ np.nan, np.nan],
[ -1., np.nan],
[ np.nan, -2.],
[ np.nan, np.nan],
[ np.nan, 0.]])
mask = (np.nan_to_num(arr) != 0).any(axis=1)
Out:
出去:
>>> arr[mask]
... array([[ -1., nan],
[ nan, -2.]])
回答by Greg
In addition: if you want to drop rows if a row has a nan or 0 in any single value
另外:如果您想删除行,如果一行在任何单个值中具有 nan 或 0
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.]])
回答by Mridul Pandey
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 ])

