1.

How will you sort the array based on the Nth column?

Answer»

For example, consider an array arr.

arr = np.array([[8, 3, 2], [3, 6, 5], [6, 1, 4]])

Let us try to sort the rows by the 2nd column so that we get:

[[6, 1, 4],[8, 3, 2],[3, 6, 5]]

We can do this by using the sort() METHOD in numpy as:

import numpy as nparr = np.array([[8, 3, 2], [3, 6, 5], [6, 1, 4]])#sort the array using np.sortarr = np.sort(arr.view('I8,i8,i8'), ORDER=['f1'], axis=0).view(np.int)

We can also perform sorting and that too inplace sorting by doing:

arr.view('i8,i8,i8').sort(order=['f1'], axis=0)


Discussion

No Comment Found