Find values in array and change them efficiently
<p>I am working with a large array with around 300 000 values. It has 100 000 rows and 3 columns. I am doing iterations with this array and if any value in the first column exceeds a limit of lets say 10, I want the number to be replaced. Is there any more efficient way than running something like this?:</p>

<pre><code>for i in range(N):
    if array[i][0]&gt;10:
        array[i][0] = 0 
</code></pre>

<p>I need to repeat this sequense for the other two columns as well which included with all my other iterations makes my code pretty slow.</p>

<p>I've assumed you may not want to use the same threshold/replacement value for each column. That being the case, you can pack the three items in a list of tuples and iterate through that.</p>

<pre><code>import numpy as np

arr = np.ndarray(your_array)
#Edited with your values, and a more than symbol
threshold = 10
column_id = 0
replace_value = 0
arr[arr[:, column_id] &gt; threshold, column_id] = replace_value
</code></pre>

<p>Set <code>threshold</code>, <code>column_id</code> and <code>replace_value</code> as you require.</p>

python arrays