How to extract rows from an numpy array based on the content?
<p>As title, for example, I have an 2d numpy array, like the one below, </p>

<pre><code>[[33, 21, 1],
 [33, 21, 2],
 [32, 22, 0],
 [33, 21, 3],
 [34, 34, 1]]
</code></pre>

<p>and I want to extract these rows orderly based on the content in the first and the second column, in this case, I want to get 3 different 2d numpy arrays, as below, </p>

<pre><code>[[33, 21, 1],
 [33, 21, 2],
 [33, 21, 3]]
</code></pre>

<p>and </p>

<pre><code>[[32, 22, 0]]
</code></pre>

<p>and </p>

<pre><code>[[34, 34, 1]]
</code></pre>

<p>What function in numpy could I use to do this? I think the point is to distinguish different rows with their first and second columns. If elements in these columns are the same, then the specific rows are categorized in the same output array. <strong>I want to write a python function to do this kind of job, because I could have a much more bigger array than the one above.</strong> Feel free to give me advice, thank you.</p>

<p>The <a href="https://github.com/EelcoHoogendoorn/Numpy_arraysetops_EP" rel="nofollow">numpy_indexed</a> package (disclaimer: I am its author) contains functionality to efficiently perform these type of operations:</p>

<pre><code>import numpy_indexed as npi
npi.group_by(a[:, :2]).split(a)
</code></pre>

python arrays numpy