简体   繁体   English

如何用另一个列表中的元素替换 numpy 数组中的 NaN

[英]How to substitute NaNs in a numpy array with elements in another list

I'm facing an issue with a basic substitution.我正面临一个基本替换的问题。 I have two arrays, one of them contains numbers and NaN, and the other one numbers that are supposed to replace the NaN, obviously ordered as I wish.我有两个数组,其中一个包含数字和 NaN,另一个是应该替换 NaN 的数字,显然按我的意愿排序。 As an example: x1 = [NaN, 2, 3, 4, 5, NaN, 7, 8, NaN, 10] and fill = [1, 6, 9] and I want to obtain by index-wise replacement an array like: x1_final = [1, 2, 3, 4, 5, NaN, 7, 8, NaN, 10]例如: x1 = [NaN, 2, 3, 4, 5, NaN, 7, 8, NaN, 10] and fill = [1, 6, 9]我想通过索引替换获得一个数组: x1_final = [1, 2, 3, 4, 5, NaN, 7, 8, NaN, 10]

I have written this idiotic line of code, which substitutes all the NaN with the first element of the fill array:我写了这行愚蠢的代码,它将所有 NaN 替换为fill数组的第一个元素:

for j in range(0,len(x1)):
    if np.isnan(x1[j]).any():
        for i in range(0,len(fill)):
            x1[j] = fill[i]

How do I manage to achieve my result?我如何设法达到我的结果?

Does this work for you?这对你有用吗?

train = np.array([2, 4, 4, 8, 32, np.NaN, 12, np.NaN])
fill = [1,3]
train[np.isnan(train)] = fill
print(train)

Output:输出:

[  2.   4.   4.   8.  32.   1.  12.   3.]

The following should work even if the size of fill doesn't match the number of nan s即使fill的大小与nan的数量不匹配,以下内容也应该有效

>>> x1 = np.random.randint(0, 4, (10,))
>>> x1 = x1/x1 + x1
>>> 
>>> x1
array([ 4., nan, nan,  4., nan,  3., nan,  2.,  3.,  4.])
>>> 
>>> fill = np.arange(3)
>>> 
>>> loc, = np.where(np.isnan(x1))
>>> 
>>> x1[loc[:len(fill)]] = fill[:len(loc)]
>>> 
>>> x1
array([ 4.,  0.,  1.,  4.,  2.,  3., nan,  2.,  3.,  4.])

The answer from @chrisz is the correct one, because you have the power of numpy, so use it :-) @chrisz 的答案是正确的,因为你有 numpy 的力量,所以使用它:-)

But if you still want to do it the way that you started, you can fix the code like this:但是如果你仍然想按照你开始的方式来做,你可以像这样修复代码:

import numpy as np

x1 = [np.NaN, 2, 3, 4, 5, np.NaN, 7, 8, np.NaN, 10] 
fill = [1, 6, 9]
i = 0

for j in range(0, len(x1)):
    if np.isnan(x1[j]).any():
        x1[j] = fill[i]
        i += 1

print x1

You were almost there, you just needed to count correctly the index of the fill (maybe adding some check for an out of bounds index).你快到了,你只需要正确计算填充的索引(也许添加一些检查越界索引)。

But, as I said, definitely go the numpy way, it's faster and cleaner.但是,正如我所说,一定要走麻木的方式,它更快更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM