[python] Get the rank of the values in List in ascending / descending order

Article content

I want to get the rank of Python List values in ascending / descending order. I found the ascending order as soon as I looked it up, but I left it as a memo because the descending order required some ingenuity.

code

It's a code right away.

from scipy.stats import rankdata

# Target data
array = [10,30,30,40,20]

# Get the rank for each index with rankdata
asc = rankdata(array)

# Display in ascending order
print(type(asc))
print(asc.astype(float))

# Descending calculation
desc = (len(asc) - asc + 1).astype(float)

# Display in descending order
print(type(desc))
print(desc)

It seems that the process of acquiring in descending order is not implemented in rankdata, so it was necessary to calculate the descending order. As a result of trying various things, I feel that this seems to be the easiest.

Below are the execution results.

<class 'numpy.ndarray'>
[1. 3. 5. 4. 2.]
<class 'numpy.ndarray'>
[5. 3. 1. 2. 4.]

If you change astype to astype (int)

<class 'numpy.ndarray'>
[1 3 5 4 2]
<class 'numpy.ndarray'>
[5 3 1 2 4]

It will be like this.

If the same value is entered in this state ...

# Target data
array = [10,30,30,40,20]
<class 'numpy.ndarray'>
[1 3 3 5 2]
<class 'numpy.ndarray'>
[5 2 2 1 4]

It will be like this. The result has changed. If it stays float

<class 'numpy.ndarray'>
[1.  3.5 3.5 5.  2. ]
<class 'numpy.ndarray'>
[5.  2.5 2.5 1.  4. ]

It will be like this.

When the data is not covered, it is a limited method.

Postscript

The method pointed out by konandoirusa is overwhelmingly easier, so I added it.

import numpy as np
from scipy.stats import rankdata
# Target data
array = [10,30,30,40,20]

# ascending order
print(rankdata(np.array(array)))
# descending order
print(rankdata(-np.array(array)))

Execution result

[1.  3.5 3.5 5.  2. ]
[5.  2.5 2.5 1.  4. ]

Recommended Posts

[python] Get the rank of the values in List in ascending / descending order
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
[python] Get the list of classes defined in the module
[Python] How to output the list values in order
Get the number of specific elements in a python list
Get the EDINET code list in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Make a copy of the list in Python
How to get the number of digits in Python
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
How to get a list of files in the same directory with python
Get the value of a specific key in a list from the dictionary type in the list with Python
Try to get the function list of Python> os package
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
How to get a list of built-in exceptions in python
Get the number of occurrences for each element in the list
Get the index of each element of the confusion matrix in Python
Get index of nth largest / smallest value in list in Python
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
Get the host name in Python
About the basics list of Python basics
Get the value of a specific key up to the specified index in the dictionary list in Python
Try to get a list of breaking news threads in Python.
python> Display 3 decimals in ".3f, .3f, .3f" format / Get 3 coordinate values in the range [-1: 1]
Get a list of files in a folder with python without a path
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
Check the behavior of destructor in Python
Get the top nth values in Pandas
Put together consecutive values in the list
Display a list of alphabets in Python 3
Get the column list & data list of CASTable
[python] Get a list of instance variables
The basics of running NoxPlayer in Python
Summary of built-in methods in Python list
scipy.sparse.linalg.svds returns singular values in ascending order
In search of the fastest FizzBuzz in Python
[Python] Get the character code of the file
[Python] Get a list of folders only
Get rid of DICOM images in Python
Get a capture of the entire web page in Selenium Python VBA
Get a list of packages installed in your current environment with python
Click the Selenium links in order to get the elements of individual pages
I want to sort a list in the order of other lists
Receive a list of the results of parallel processing in Python with starmap
Get a datetime instance at any time of the day in Python
Get the key for the second layer migration of JSON data in python
Get the contents of git diff from python
Output the number of CPU cores in Python
[python] Check the elements of the list all, any
Get the weather in Osaka via WebAPI (python)
[Python] Get / edit the scale label of the figure
View the result of geometry processing in Python
Get only the subclass elements in a list