convert nan value to zero

前端 未结 9 1006
暖寄归人
暖寄归人 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条回答
  •  萌比男神i
    2020-12-01 00:22

    For your purposes, if all the items are stored as str and you just use sorted as you are using and then check for the first element and replace it with '0'

    >>> l1 = ['88','NaN','67','89','81']
    >>> n = sorted(l1,reverse=True)
    ['NaN', '89', '88', '81', '67']
    >>> import math
    >>> if math.isnan(float(n[0])):
    ...     n[0] = '0'
    ... 
    >>> n
    ['0', '89', '88', '81', '67']
    

提交回复
热议问题