更新时间:2023-11-25 10:52:46
这是一种方法 -
mask = np.isnan(arr)
idx = np.where(~mask,np.arange(mask.shape[1]),0)
np.maximum.accumulate(idx,axis=1, out=idx)
out = arr[np.arange(idx.shape[0])[:,None], idx]
如果您不想创建另一个数组而只想填充arr
本身中的NaN,请将最后一步替换为-
If you don't want to create another array and just fill the NaNs in arr
itself, replace the last step with this -
arr[mask] = arr[np.nonzero(mask)[0], idx[mask]]
样本输入、输出-
In [179]: arr
Out[179]:
array([[ 5., nan, nan, 7., 2., 6., 5.],
[ 3., nan, 1., 8., nan, 5., nan],
[ 4., 9., 6., nan, nan, nan, 7.]])
In [180]: out
Out[180]:
array([[ 5., 5., 5., 7., 2., 6., 5.],
[ 3., 3., 1., 8., 8., 5., 5.],
[ 4., 9., 6., 6., 6., 6., 7.]])