在 Python 中用 0 替换 NaN

Replacing NaN with 0 in Python

我有一些数据到处都是缺失值。我需要用零替换 NaN,因为我对名为 ls 的列表中的那些元素进行数学运算。我尝试了列表理解,但没有成功:

[0 if i==None else i for i in ls]  

感谢help/suggestions!

尝试以下操作:

from numpy import isnan

[0 if isnan(i) else i for i in ls]

这应该有效:

import math
[0 if math.isnan(i) else i for i in ls]