1

Can I sort the rows or columns of an array according to values stored in a separate list?

For example:

row_keys = [10, 11, 5, 6]
z = np.array([[2.77, 11., 4.1, 7.2],
                      [3.7, 2.2, 1.1, 0.5],
                      [2.5, 3.5, 5.0, 9.0],
                      [4.3, 2.2, 5.1, 6.1]])

Should produce something like

array([[  2.5,   3.5,   5. ,   9. ],
       [  4.3,   2.2,   5.1,   6.1]
       [  2.77,  11. ,   4.1,   7.2],
       [  3.7,   2.2,   1.1,   0.5],
       ])

And similar functionality applied to the columns, please.

1 Answer 1

1

Another way for rows

z_rows = z[np.argsort(row_keys)]

and for columns

z_columns = z.T[np.argsort(row_keys)].T
Sign up to request clarification or add additional context in comments.

1 Comment

This is the right way. Much more compact and faster than using a list comprehension.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.