Write a program in Python to find the minimum rank of a particular column in a dataframe



Solution

Assume, you have a dataframe and minimum rank of a particular column,

 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

To solve this, we will follow the steps given below −

  • Define a dataframe.

  • Assign df[‘Age’] column inside rank function to calculate the minimum rank for axis 0 is,

df["Age"].rank(axis=0,method ='min',ascending=True)

Example

Let’s see the following code to get a better understanding −

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(df)

Output

 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
Updated on: 2021-02-25T05:33:26+05:30

360 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements