How to use numpy with 'None' value in Python?

后端 未结 7 2362
甜味超标
甜味超标 2020-12-18 18:14

I\'d like to calculate the mean of an array in Python in this form:

Matrice = [1, 2, None]

I\'d just like to have my None valu

7条回答
  •  再見小時候
    2020-12-18 18:28

    You can 'upcast' the array to numpy's float64 dtype and then use numpy's nanmean method as in the following example:

    import numpy as np
    
    arr = [1,2,3, None]
    arr2 = np.array(arr, dtype=np.float64)
    print(arr2) # [ 1.  2.  3. nan]
    print(np.nanmean(arr2)) # 2.0
    

提交回复
热议问题