matlan 将NaN转化为0
可以使用numpy库中的isnan()函数来判断是否为NaN,然后使用numpy库中的nan_to_num()函数将NaN转化为0。
示例代码:
import numpy as np
a = np.array([1, 2, np.nan, 4, np.nan])
a[np.isnan(a)] = 0
a = np.nan_to_num(a)
print(a)
输出:
[1. 2. 0. 4. 0.]
原文地址: https://www.cveoy.top/t/topic/cIuk 著作权归作者所有。请勿转载和采集!