擅长:python、mysql、java
<p>以下是一个仅适用于NumPy的解决方案:</p>
<pre><code>In [51]: arr
Out[51]:
array([[0.5, 1. ],
[0. , 0. ]])
# generate a boolean mask
In [52]: mask = (arr < 1)
Out[52]:
array([[ True, False],
[ True, True]])
# go over all the columns and check for condition
In [53]: for col in range(mask.shape[1]):
...: if np.all(mask[:, col]):
...: arr[:, col] = arr[:, col] + arr[:, col+1]
...: else:
...: continue
In [54]: arr
Out[54]:
array([[1.5, 1. ],
[0. , 0. ]])
</code></pre>
<p>注意:这种方法假设特定列中的所有值都小于右侧的值。你知道吗</p>