In NumPy, a two-dimensional array is sorted according to a column and a row _python

Source: Internet
Author: User
The following for everyone to share a numpy in the implementation of a two-dimensional array in accordance with a column, a row of the method, has a good reference value, I hope to be helpful to everyone. Come and see it together.

How do I sort by a row in a two-dimensional array or a column? Suppose that data is a two-dimensional array of type Numpy.array, which can be implemented using the Argsort function in NumPy, with the following code example:

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 row, you can take advantage of the transpose operation, as shown in the code below:

data = data. T (data. T[:,2].argsort ()). T # Sort Columns According to line 3rd

You can also sort directly by line, with the following code:

data = Data[:,data[2].argsort ()]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.