How to extract arrays from an arranged numpy array?
<p>This is a relative question of the post <a href="http://stackoverflow.com/questions/39673377/how-to-extract-rows-from-an-numpy-array-based-on-the-content/39674145?noredirect=1#comment66837793_39674145">How to extract rows from an numpy array based on the content?</a>, and I used the following code to split rows based on the content in the column:</p>

<pre><code>np.split(sorted_a,np.unique(sorted_a[:,1],return_index=True)[1][1:])
</code></pre>

<p>the code worked fine, but later I tried the code to split other cases (as below), I found that there could be wrong results (as showed in CASE#1).</p>

<pre><code>CASE#1
[[2748309, 246211, 1],
 [2748309, 246211, 2],
 [2747481, 246201, 54]]
OUTPUT#1
[]
[[2748309, 246211, 1],
 [2748309, 246211, 2],
 [2747481, 246201, 54]]
the result I want
[[2748309, 246211, 1],
 [2748309, 246211, 2]]
[[2747481, 246201, 54]]
</code></pre>

<p>I think the code may successfully split rows only in the case with little numbers, which with less digits, and I don't know how to solve problems showed in CASE#1 above. So in this post, I have 2 little relative questions:</p>

<p><strong>1. How to split rows with greater numbers in it? (as showed in CASE #1)?</strong></p>

<p><strong>2. How to handle (split) data with both cases including #1 rows with the same element in the second column, but different in the first, and #2 rows with the same element in the first column, but different in the second ? (That is, could python distinguish rows considering contents in both first and second columns simultaneously?)</strong></p>

<p>Feel free to give me suggestions, thank you.</p>

<p><strong>Update#1</strong></p>

<p>The <code>ravel_multi_index</code> function could handle this kind of task with integer-arrays, but how to deal with arrays containing float?</p>

<p>If I apply that split line directly to your array I get your result, an empty array plus the original</p>

<pre><code>In [136]: np.split(a,np.unique(a[:,1],return_index=True)[1][1:])
Out[136]: 
[array([], shape=(0, 3), dtype=int32), 
 array([[2748309,  246211,       1],
        [2748309,  246211,       2],
        [2747481,  246201,      54]])]
</code></pre>

<p>But if I first sort the array on the 2nd column, as specified in the linked answer, I get the desired answer - with the 2 arrays switched</p>

<pre><code>In [141]: sorted_a=a[np.argsort(a[:,1])]
In [142]: sorted_a
Out[142]: 
array([[2747481,  246201,      54],
       [2748309,  246211,       1],
       [2748309,  246211,       2]])
In [143]: np.split(sorted_a,np.unique(sorted_a[:,1],return_index=True)[1][1:])
Out[143]: 
[array([[2747481,  246201,      54]]), 
 array([[2748309,  246211,       1],
        [2748309,  246211,       2]])]
</code></pre>

python arrays numpy