# sortrows with multiple sorting keys in numpy

• Last Update :
• Techknowledgy :

The syntax is quite unwieldy and looks weird, but the cleanest thing to do is np.lexsort.

data = np.array([
[3, 0, 0, .24],
[4, 1, 1, .41],
[2, 1, 1, .63],
[1, 1, 3, .38]
]) #imagine rows of a spreadsheet
#now do sortrows(data, [3, -4])
ix = np.lexsort((data[: , 3][::-1], data[: , 2]))
#this yields[0, 2, 1, 3]

#note that lexsort sorts first from the last row, so sort keys are in reverse order

data[ix]

EDIT2: as negative inicies in python have meaning, I think they should not be used to specify descending order for the column, therefore I used here an auxiliary Descending-object.

import numpy as np

class Descending:
""
" for np_sortrows: sort column in descending order "
""
def __init__(self, column_index):
self.column_index = column_index

def __int__(self): # when cast to integer
return self.column_index

def np_sortrows(M, columns = None):
""
"  sorting 2D matrix by rows: param M: 2 D numpy array to be sorted by rows: param columns: None
for all columns to be used,
iterable of indexes or Descending objects: return: returns sorted M ""
"
if len(M.shape) != 2:
raise ValueError('M must be 2d numpy.array')
if columns is None: # no columns specified, use all in reversed order
M_columns = tuple(M[: , c]
for c in range(M.shape[1] - 1, -1, -1))
else:
M_columns = []
for c in columns:
M_c = M[: , int(c)]
if isinstance(c, Descending):
M_columns.append(M_c[::-1])
else:
M_columns.append(M_c)
M_columns.reverse()

return M[np.lexsort(M_columns),: ]

data = np.array([
[3, 0, 0, .24],
[4, 1, 1, .41],
[2, 1, 3, .25],
[2, 1, 1, .63],
[1, 1, 3, .38]
])

# third column is index 2, fourth column in reversed order at index 3
print(np_sortrows(data, [2, Descending(3)]))

Suggestion : 2

NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value. The kind argument of the argsort function makes it possible to sort arrays on multiple rows or columns. This article will go through sorting single columns and rows and sorting multiple columns using simple examples with code.,Sometimes it’s necessary to sort on more than one column. One example of this would be with data that have year, month, day, and value in separate columns. NumPy’s argsort can handle sorting multiple columns using the kind argument. ,A NumPy array can also be sorted by row values. This is accomplished in the same way as sorting with columns. We just need to change the indexing positions. For example, let’s take the array we created that’s sorted on the first column, then sort the columns by values in the first row. ,As you can see, the rows are now ordered least to greatest according to the first column. To sort on a different column, simply change the column index. For example, we can sort on column 2 with a[a[:, 1].argsort()].

1._
import numpy

Now create an array of random integers. Here, I’m creating an array with 5 rows and 4 columns. The values in the array are random, so you will get different values than those shown here if you use the same code. If you’re not familiar with the basics of creating numpy arrays and numpy array shapes, check out this article.

a = np.random.randit(100, size = (5, 4))

output: [
[44 47 64 67]
[67 9 83 21]
[36 87 70 88]
[88 12 58 65]
[39 87 46 88]
]
3._
a[: , 0].argsort()

output: [2 4 0 1 3]

To do this, simply move the index (0) to the row position and move the argsort result to the column position. The code below shows the demonstration and result.

a = a[: , a[0,: ].argsort()]

output: [
[36 70 87 88]
[39 46 87 88]
[44 64 47 67]
[67 83 9 21]
[88 58 12 65]
]

Let’s start by creating an array with 4 columns that represent, year, month, day, and a value.

b = np.array([
[2020, 1, 1, 98],
[2020, 2, 1, 99],
[2021, 3, 6, 43],
[2020, 2, 1, 54],
[2021, 1, 1, 54],
[2020, 1, 2, 74],
[2021, 1, 3, 87],
[2021, 3, 9, 23]
])

Suggestion : 3

The various sorting algorithms are characterized by their average speed, worst case performance, work space size, and whether they are stable. A stable sort keeps items with the same key in the same relative order. The four algorithms implemented in NumPy have the following properties:,where R is a non-nan real value. Complex values with the same nan placements are sorted according to the non-nan part if it exists. Non-nan values are sorted as before.,Indirect stable sort on multiple keys.,Sorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort or radix sort under the covers and, in general, the actual implementation will vary with data type. The ‘mergesort’ option is retained for backwards compatibility.

>>> a = np.array([
[1, 4],
[3, 1]
]) >>>
np.sort(a) # sort along the last axis
array([
[1, 4],
[1, 3]
]) >>>
np.sort(a, axis = None) # sort the flattened array
array([1, 1, 3, 4]) >>>
np.sort(a, axis = 0) # sort along the first axis
array([
[1, 1],
[3, 4]
])
>>> dtype = [('name', 'S10'), ('height', float), ('age', int)] >>>
values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),
] >>>
a = np.array(values, dtype = dtype) # create a structured array >>>
np.sort(a, order = 'height')
array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
('Lancelot', 1.8999999999999999, 38)
],
dtype = [('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
>>> np.sort(a, order = ['age', 'height'])
array([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),
('Arthur', 1.8, 41)
],
dtype = [('name', '|S10'), ('height', '<f8'), ('age', '<i4')])

Suggestion : 4

In this article we will discuss how to sort a 2D Numpy array by single or multiple rows or columns.,On the similar logic we can sort a 2D Numpy array by a single row i.e. shuffle the columns of 2D numpy array to make the given row sorted.,Its logic was similar to above i.e. Select row at given index position using [] operator and then get sorted indices of this row using argsort(). ,Let’s sort the above created 2D Numpy array by 2nd row i.e. row at index position 1 i.e.

First of all import numpy module i.e.

import numpy as np

# Create a 2 D Numpy array list of list
arr2D = np.array([
[11, 12, 13, 22],
[21, 7, 23, 14],
[31, 10, 33, 7]
])

print('2D Numpy Array')
print(arr2D)

columnIndex = 1
# Sort 2 D numpy array by 2n d Column
sortedArr = arr2D[arr2D[: , columnIndex].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)

Suppose we have a 2D Numpy array i.e.

# Create a 2 D Numpy array list of list
arr2D = np.array([
[11, 12, 13, 22],
[21, 7, 23, 14],
[31, 10, 33, 7]
])

print('2D Numpy Array')
print(arr2D)

Let’s sort the above created 2D Numpy array by 2nd row i.e. row at index position 1 i.e.

# Sort 2 D numpy array by 2n d row
sortedArr = arr2D[: , arr2D[1].argsort()]

print('Sorted 2D Numpy Array')
print(sortedArr)