Monday, October 6, 2025
HomeLanguagesRank Values in NumPy Array

Rank Values in NumPy Array

When working with data in NumPy arrays, it’s often necessary to rank the elements based on certain criteria. Ranking can be useful for tasks like finding the largest or smallest values, identifying outliers, or sorting data for further analysis. In this article, we are going to see how to rank items in Numpy arrays in Python.

Rank Items in Python NumPy Array:

Below are the ways by which we can rank items in NumPy Array:

Rank Items using argsort() function

In this example, we have used a 1-D array to rank items in Python NumPy Array using argsort() function.

Python3




# Python program to rank items in
# NumPy array using argsort function
 
# Import the library numpy
import numpy as np
 
# Define the NumPy array
arr = np.array([[1, 2, 3],[5, 6, 4], [9, 8, 7]])
 
# Rank items in NumPy array using argort() function
rank = np.array(arr).argsort().argsort()
 
# Print the rank of elements
print(rank)


Output

[3 1 4 0 5 2]

Rank Items in 2-D Array

In this example, we have used a 2-D array to rank items in NumPy Array using argsort() function.

Python3




# Python program to rank items in
# NumPy array using rankdata function
 
# Import the libraries numpy and rankdata
import numpy as np
from scipy.stats import rankdata
 
# Define the NumPy array
arr = np.array([7, 4, 13, 2, 19, 5])
 
# Calculate rank of each item in array
rank_items = rankdata(arr)
 
# Print the rank of each element
print(rank_items)


Output

[[0 1 2 3 4]
[1 2 3 4 0]]

Rank Items in 3-D Array

In this example, we have used a 3-D array to rank items in NumPy Array using argsort() function.

Python3




# Python program to rank items in
# NumPy array using rankdata function
 
# Import the libraries numpy and rankdata
import numpy as np
from scipy.stats import rankdata
 
# Define the NumPy array
arr = np.array([[1, 2, 3, 4, 5],[6, 7, 8, 9, 0]])
 
#calculate rank of each row in array
rank0 = rankdata(arr[0])
rank1= rankdata(arr[1])
 
# Combine rank of each row to form 2D array
rank=np.row_stack((rank0,rank1))
 
# Print the rank of each element
print(rank)


Output

[[0 1 2]
[1 2 0]
[2 1 0]]

Rank Items using rankdata() in Python

In this example, we have used a 1-D array to rank items in NumPy Array using rankdata() in Python.

Python3




# Python program to rank items in
# NumPy array using rankdata function
 
# Import the libraries numpy and rankdata
import numpy as np
from scipy.stats import rankdata
 
# Define the NumPy array
arr = np.array([[1, 2, 3],[5, 6, 4], [9, 8, 7]])
 
#calculate rank of each row in array
rank0 = rankdata(arr[0])
rank1= rankdata(arr[1])
rank2= rankdata(arr[2])
 
# Combine rank of each row to form 2D array
rank=np.row_stack((rank0,rank1,rank2))
 
# Print the rank of each element
print(rank)


Output

[4. 2. 5. 1. 6. 3.]

Rank Items in 2D Array

In this example, we have used a 2-D array to rank items in NumPy Array using rankdata() function.

Python3





Output

[[1. 2. 3. 4. 5.]
[2. 3. 4. 5. 1.]]

Rank Items in 3D Array

In this example, we have used a 3-D array to rank items in NumPy Array.

Python3




# Python program to rank items in
# NumPy array using rankdata function
 
# Import the libraries numpy and rankdata
import numpy as np
from scipy.stats import rankdata
 
# Define the NumPy array
arr = np.array([[1, 2, 3],[5, 6, 4], [9, 8, 7]])
 
#calculate rank of each row in array
rank0 = rankdata(arr[0])
rank1= rankdata(arr[1])
rank2= rankdata(arr[2])
 
# Combine rank of each row to form 2D array
rank=np.row_stack((rank0,rank1,rank2))
 
# Print the rank of each element
print(rank)


Output

[[1. 2. 3.]
[2. 3. 1.]
[3. 2. 1.]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6781 POSTS0 COMMENTS