How to sort by specifying a column in the Python Numpy array.

Notes for Python beginners. Please be careful about mistakes in the content.
import numpy as np

def practice():
    data = np.array([[10,9],[8,7],[6,5],[4,3],[2,1]])
    
    print "Before:"
    print data 
    print 

Sort the last column with # argsort () and sort in order

Change'-1'to a specified column if necessary

    data = data[data[:,-1].argsort()]
    
    print "After:" 
    print data

if __name__ == '__main__':
    practice()
    pass

Output:

Before: [[10 9] [ 8 7] [ 6 5] [ 4 3] [ 2 1]]

After: [[ 2 1] [ 4 3] [ 6 5] [ 8 7] [10 9]]

Recommended Posts

How to sort by specifying a column in the Python Numpy array.
How to use loc / iloc / ix to get by specifying a column in CASTable
How to slice a block multiple array from a multiple array in Python
How to use the __call__ method in a Python class
How to get the last (last) value in a list in Python
How to execute a schedule by specifying the Python time zone and execution frequency
How to get a stacktrace in python
Create a python numpy array
How to sort by specifying a column in the Python Numpy array.
Why Python slicing is represented by a colon (:)
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
How to use the C library in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to erase the characters output by Python
How to notify a Discord channel in Python
How to get the files in the [Python] folder
Output in the form of a python array
[Python] How to draw a histogram in Matplotlib
[Python] How to sort instances by instance variables
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
[Python] Manipulating elements in a list (array) [Sort]
How to pass the execution result of a shell command in a list in Python
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
How to read all the classes contained in * .py in the directory specified by Python
How to get a list of files in the same directory with python
How to retrieve the nth largest value in Python
How to convert / restore a string with [] in python
How to get the variable name itself in python
Various ways to extract columns in a NumPy array
How to get the number of digits in Python
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
How to know the current directory in Python in Blender
[Python] How to expand variables in a character string
[Python] How to sort dict in list and instance in list
How to use the model learned in Lobe in Python
How to execute a command using subprocess in Python
[Python] How to output the list values in order
Create a python numpy array
How to develop in Python
Sort by date in python
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
Let's make a leap in the manufacturing industry by utilizing the Web in addition to Python
How to find the first element that matches your criteria in a Python list
[python] How to check if the Key exists in the dictionary
How to switch the configuration file to be read by Python
How to debug the Python standard library in Visual Studio
How to convert an array to a dictionary with Python [Application]
How to swap elements in an array in Python, and how to reverse an array.
A story about how to specify a relative path in python.
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
How to transpose a 2D array using only python [Note]
How to define multiple variables in a python for statement
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]