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

不言
Release: 2018-04-04 16:48:36
Original
6415 people have browsed it

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列对行排序
Copy after login

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行对列进行排序
Copy after login

You can also sort directly by row,The code is as follows:

data = data[:,data[2].argsort()]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!