我正在使用Python Numpy数组(特别是转换为2D数组的栅格),我想要做的是使用一个具有-999的任意虚拟值的数组,表示"没有数据"我想用相应的" real"替换这些值。来自正确位置的相同大小和形状的不同数组的值。我无法找到一个非常相似的问题,但请注意我是Python和Numpy的新手。
但我想做的是:
array_a =
([[0.564,-999,-999],
[0.234,-999,0.898],
[-999,0.124,0.687],
[0.478,0.786,-999]])
array_b =
([[0.324,0.254,0.204],
[0.469,0.381,0.292],
[0.550,0.453,0.349],
[0.605,0.582,0.551]])
使用array_b的值填充array_a中的-999值并创建一个新数组:
new_array_a =
([[0.564,0.254,0.204],
[0.234,0.381,0.898],
[0.550,0.124,0.687],
[0.478,0.786,0.551]])
我真的不想改变阵列的形状或尺寸,因为我之后会转换回栅格,所以我需要在正确的位置找到正确的值。 这样做的最佳方式是什么?
答案 0 :(得分:8)
只需做布尔屏蔽:
mask = (array_a == -999)
new_array = np.copy(array_a)
new_array[mask] = array_b[mask]
答案 1 :(得分:0)
您需要做的就是
array_a[array_a==-999]=array_b[array_a==-999]
我们将布尔条件放在数组元素上以更新其值应为-999
import numpy as np
array_a =np.array([[0.564,-999,-999],
[0.234,-999,0.898],
[-999,0.124,0.687], [![enter image description here][1]][1]
[0.478,0.786,-999]])
array_b =np.array([[0.324,0.254,0.204],
[0.469,0.381,0.292],
[0.550,0.453,0.349],
[0.605,0.582,0.551]])
array_a[array_a==-999]=array_b[array_a==-999]
运行此代码段