Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to find the minimum rank of a particular column in a dataframe
Finding the minimum rank of values in a DataFrame column is useful for data analysis and ranking operations. Pandas provides the rank() method with different ranking strategies including the minimum rank approach.
Understanding Minimum Rank
Minimum rank assigns the lowest possible rank to tied values. For example, if two values tie for 1st place, both get rank 1, and the next rank becomes 3 (skipping rank 2).
Creating the DataFrame
Let's start by creating a sample DataFrame with age data ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5],
'Name': ["Adam", "David", "Michael", "Peter", "William"],
'Age': [12, 13, 14, 12, 13]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Original DataFrame: Id Name Age 0 1 Adam 12 1 2 David 13 2 3 Michael 14 3 4 Peter 12 4 5 William 13
Calculating Minimum Rank
Use the rank() method with method='min' parameter to calculate minimum ranks ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5],
'Name': ["Adam", "David", "Michael", "Peter", "William"],
'Age': [12, 13, 14, 12, 13]}
df = pd.DataFrame(data)
df["Rank"] = df["Age"].rank(axis=0, method='min', ascending=True)
print("DataFrame with Minimum Rank:")
print(df)
DataFrame with Minimum Rank: Id Name Age Rank 0 1 Adam 12 1.0 1 2 David 13 3.0 2 3 Michael 14 5.0 3 4 Peter 12 1.0 4 5 William 13 3.0
How It Works
In the ranking process:
Ages 12 (Adam and Peter) both get rank 1.0 (minimum rank for tied values)
Ages 13 (David and William) both get rank 3.0 (next available rank after skipping 2)
Age 14 (Michael) gets rank 5.0 (next available rank after skipping 4)
Getting Just the Rank Values
To get only the rank series without adding to DataFrame ?
import pandas as pd
data = {'Id': [1, 2, 3, 4, 5],
'Name': ["Adam", "David", "Michael", "Peter", "William"],
'Age': [12, 13, 14, 12, 13]}
df = pd.DataFrame(data)
age_ranks = df["Age"].rank(axis=0, method='min', ascending=True)
print("Age column minimum ranks:")
print(age_ranks)
Age column minimum ranks: 0 1.0 1 3.0 2 5.0 3 1.0 4 3.0 Name: Age, dtype: float64
Parameters Explained
axis=0: Rank along rows (default behavior)
method='min': Assign minimum rank to tied groups
ascending=True: Lower values get lower ranks
Conclusion
Use df["column"].rank(method='min') to calculate minimum ranks for tied values in a DataFrame column. This method ensures tied values receive the lowest possible rank in their group.
