convert nan value to zero

前端 未结 9 991
暖寄归人
暖寄归人 2020-11-30 23:28

I have a 2D numpy array. Some of the values in this array are NaN. I want to perform certain operations using this array. For example consider the array:

<
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 00:32

    This should work:

    from numpy import *
    
    a = array([[1, 2, 3], [0, 3, NaN]])
    where_are_NaNs = isnan(a)
    a[where_are_NaNs] = 0
    

    In the above case where_are_NaNs is:

    In [12]: where_are_NaNs
    Out[12]: 
    array([[False, False, False],
           [False, False,  True]], dtype=bool)
    

提交回复
热议问题