5

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?

0

1 Answer 1

3

You can use np.argsort:

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

array([[12, 35,  1,  6],
       [ 1,  2,  3,  4],
       [ 1,  2,  6,  3]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.