4

For instance,

In [11]: X  = np.array([[1,2,3,4],[1,2,6,3],[12,35,1,6]])

which gives

In [12]: X
Out[12]: 
array([[ 1,  2,  3,  4],
       [ 1,  2,  6,  3],
       [12, 35,  1,  6]])

Now If i sort this using

In [13]: X.sort(axis=0)

In [14]: X
Out[14]: 
array([[ 1,  2,  1,  3],
       [ 1,  2,  3,  4],
       [12, 35,  6,  6]])

I lose the row structure. All I want to do is sort one column at a time and maintain the row structure. So

Ordering w.r.t the 3rd column

In [14]: X
Out[14]: 
array([[ 12,  35,  1,  6],
       [ 1,  2,  3,  4],
       [1, 2,  6,  3]])

the third column is in order and the row is maintained.

How do I achieve this using numpy?

jpp
  • 159,742
  • 34
  • 281
  • 339
ZingyMcGhee
  • 71
  • 1
  • 5

1 Answers1

3

You can use np.argsort:

Y = X[X[:, 2].argsort()]

array([[12, 35,  1,  6],
       [ 1,  2,  3,  4],
       [ 1,  2,  6,  3]])
jpp
  • 159,742
  • 34
  • 281
  • 339