簡體   English   中英

我如何用其他2d數組值替換2d數組中的nan

[英]How do i replace nan in 2d array with other 2d array value

例如,我在名為c的二維數組上。

>>> c = numpy.array([[1,np.nan,3],[4,5,6],[7,8,9]])
>>> c
array([[  1.,  nan,   3.],
       [  4.,   5.,   6.],
       [  7.,   8.,   9.]])

其他命名為b。

>>> b
array([[1, 0, 1],
       [0, 0, 0],
       [1, 0, 1]])

在數組索引c [0] [1]中,有一個我想將其替換為b [0] [1]。 不使用for循環。

numpy中是否有任何方法可以使我做到這一點?

我希望結果如下所示。

>>> c
array([[ 1.,  0.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

您可以使用numpy.isnanhttp://docs.scipy.org/doc/numpy-1.10.1/reference/generation/numpy.isnan.html

c = numpy.array([[1,numpy.nan,3],[4,5,6],[7,8,9]])
b = numpy.array([[1,0,1],[0,0,0],[1,0,1]])
ind = numpy.isnan(c)
c[ind] = b[ind]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM