Iterate over numpy array
<p>Given the following array:</p>

<pre><code>x = np.array([[0,2,4,5,5.5,6,7],[4,5,6,7,2,3,4]])
</code></pre>

<p>Based on that array I need to create another array that skip the rows unit the value in the first column is >5.</p>

<p>So the result should like like this:</p>

<pre><code>([[5.5,6,7],[2,3,4]])
</code></pre>

<p>Any hints for a simple (and fast) method for that problem?
Thank you very much for your help!</p>

<p>We could use a <a href="http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays" rel="nofollow">boolean array</a> as index for filtering.</p>

<pre><code>&gt;&gt;&gt; x[:, x[0] &gt; 5]
array([[ 5.5,  6. ,  7. ],
       [ 2. ,  3. ,  4. ]])
</code></pre>

<ul>
<li><code>x[0]</code> selects the first row</li>
<li><code>x[0] &gt; 5</code> creates an array of boolean, checking whether an element is > 5 or not. (This is <code>[False, False, False, False, True, True, True]</code>.)</li>
<li><p>When we write <code>some_array[boolean_array]</code>, we  only keep the elements in <code>some_array</code> which the corresponding value in <code>boolean_array</code> is True. For instance, </p>

<pre><code>&gt;&gt;&gt; numpy.array([2, 4, 6, 8])[numpy.array([True, False, False, True])]
array([2, 8])
</code></pre></li>
<li><p>Since we are going to select columns, the boolean array <code>x[0] &gt; 5</code> should be placed in the second axis. We select the whole first axis with <code>:</code>. Thus the final expression is <code>x[:, x[0] &gt; 5]</code>.</p></li>
</ul>

python numpy