在numpy数组中删除NaN值

6

我有两个包含NaN的numpy数组:

A = np.array([np.nan,   2,   np.nan,   3,   4])
B = np.array([   1  ,   2,     3   ,   4,  np.nan])

有没有使用numpy一种聪明的方法来删除两个数组中的NaN,并且删除另一个列表中相应索引处的内容?使其看起来像这样:

A = array([  2,   3, ])
B = array([  2,   4, ])

你也在使用 pandas 吗?(有一个 dropna 可以对 DataFrame 对象进行所需的操作) - Kos
2个回答

10

您可以将这两个数组相加,这样就会在没有值的地方覆盖为NaN值,然后使用它来生成一个布尔掩码索引,然后使用该索引来索引到原始的numpy数组中:

In [193]:

A = np.array([np.nan,   2,   np.nan,   3,   4])
B = np.array([   1  ,   2,     3   ,   4,  np.nan])
idx = np.where(~np.isnan(A+B))
idx
print(A[idx])
print(B[idx])
[ 2.  3.]
[ 2.  4.]

A+B 的输出结果为:

In [194]:

A+B
Out[194]:
array([ nan,   4.,  nan,   7.,  nan])

编辑

正如 @Oliver W. 正确指出的那样,np.where 是不必要的,因为 np.isnan 会生成一个布尔索引,您可以使用它来索引数组:

In [199]:

A = np.array([np.nan,   2,   np.nan,   3,   4])
B = np.array([   1  ,   2,     3   ,   4,  np.nan])
idx = (~np.isnan(A+B))
print(A[idx])
print(B[idx])
[ 2.  3.]
[ 2.  4.]

1
使用 np.where 完全是多余的。 - Oliver W.
@OliverW。嗯,你说得对,我最初认为我需要整数索引,但在这里是不必要的,我会更新我的答案。 - EdChum

8

A[~(np.isnan(A) | np.isnan(B))]

这行代码的意思是从数组A中选择那些在A和B两个数组中都不为NaN的元素。

B[~(np.isnan(A) | np.isnan(B))]

这行代码的意思是从数组B中选择那些在A和B两个数组中都不为NaN的元素。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接