0

I have a 2D numpy array like this

[[ 569  897]
 [ 570  898]
 [ 570  900]
 [ 571  901]
 [ 571  905]
 [ 572  906]]

I want the elements which have equal values in the first column to be grouped together in the following way.

[[  569  897]
 [[ 570  898]
  [ 570  900]]
 [[ 571  901]
  [ 571  905]]
 [  572  906] ]

How should i do so?

hulkinBrain
  • 746
  • 8
  • 28

1 Answers1

4

You can use np.unique to get the separating indices and then use np.split to actually split -

np.split(a, np.unique(a[:,0], return_index=1)[1][1:],axis=0)

Alternatively, with slicing and using np.flatnonzero -

np.split(a, np.flatnonzero(a[1:,0] != a[:-1,0])+1,axis=0)

Sample run -

In [63]: a
Out[63]: 
array([[569, 897],
       [570, 898],
       [570, 900],
       [571, 901],
       [571, 905],
       [572, 906]])

In [64]: out = np.split(a, np.flatnonzero(a[1:,0] != a[:-1,0])+1,axis=0)

In [65]: out[0]
Out[65]: array([[569, 897]])

In [66]: out[1]
Out[66]: 
array([[570, 898],
       [570, 900]])

In [67]: out[2]
Out[67]: 
array([[571, 901],
       [571, 905]])

In [68]: out[3]
Out[68]: array([[572, 906]])
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • Could you provide an example for your proposed method? It isn't working as i want it to. – hulkinBrain Feb 03 '17 at 20:36
  • @hulkinBrain Well I assumed the first column to be sorted, am I correct to assume that? It should work then, otherwise could you provide such a sample? – Divakar Feb 03 '17 at 20:41
  • Yeah first column is sorted. – hulkinBrain Feb 03 '17 at 20:43
  • @hulkinBrain Added one. – Divakar Feb 03 '17 at 20:46
  • Yes, it did produce the output. I made a silly mistake, my bad. Thanks! Can you help me out with another problem of mine? This one has been bugging me for many hours. http://stackoverflow.com/questions/42027261/filtering-coordinate-points-on-the-basis-of-nearest-distance – hulkinBrain Feb 03 '17 at 20:48
  • What if the elements are not sorted? What if the elements are like this: `[ 569, 897], [ 570, 898], [ 570, 900], [ 571, 901], [ 571, 905], [ 572, 906], [ 570, 898], [ 570, 900]` – hulkinBrain Feb 04 '17 at 19:28
  • 1
    @hulkinBrain Lexsort it : `a[np.lexsort(a.T)]`. – Divakar Feb 04 '17 at 19:59