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
Selected Reading
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
Advertisements
