Below I will share with you an article on how to sort a two-dimensional array according to a certain column and a certain row in numpy. It has a good reference value and I hope it will be helpful to everyone. Let's take a look together
How to sort according to a certain row or column in a two-dimensional array? Assume that data is a two-dimensional array of type numpy.array, which can be implemented using the argsort function in numpy. The code example is as follows:
data = data[data[:,2].argsort()] #按照第3列对行排序
Note: argsort returns only the sorted row index and does not change the original array.
To sort by a certain row, you can use the transpose operation. The code is as follows:
data = data.T(data.T[:,2].argsort()).T # 按照第3行对列进行排序
You can also sort directly by row,The code is as follows:
data = data[:,data[2].argsort()]
Related recommendations:
A brief discussion on several sorting methods of numpy arrays_python
Python numpy function linspace implementation of creating an arithmetic sequence sharing example
Python method to create a symmetric matrix based on the numpy module
The above is the detailed content of How to sort a two-dimensional array according to a certain column and a certain row in numpy_python. For more information, please follow other related articles on the PHP Chinese website!