Python numpy sort by specified row or column

The method used in this article is from the link below, thanks!
Sorting arrays in NumPy by column
arr = np.array([[1,3,5],[4,2,0]])

print(f'arr\n{arr}')

# 按第2列排序(column index = 1)
print('按第2列排序\n',arr[arr[:,1].argsort()])

# 按第2行排序(row index = 1)
print('按第2行排序\n',arr[:,arr[1,:].argsort()])
arr 
[[1 3 5] 
 [4 2 0]] 
Sort by column 2 
 [[4 2 0] 
 [1 3 5]] 
Sort by row 2 
 [[5 3 1] 
 [0 2 4]]

 

Attached a schematic diagram of DataFrame sorting

 

Guess you like

Origin blog.csdn.net/authorized_keys/article/details/111684383