Home >Backend Development >Python Tutorial >How to sort a two-dimensional array according to a certain column and a certain row in numpy_python

How to sort a two-dimensional array according to a certain column and a certain row in numpy_python

不言
不言Original
2018-04-04 16:48:366507browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn