0
0
Pandasdata~20 mins

rank() method and ranking methods in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rank Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of rank() with default method
What is the output of the following code snippet using pandas rank() method with default parameters?
Pandas
import pandas as pd
s = pd.Series([3, 1, 4, 1, 5])
ranks = s.rank()
print(ranks.tolist())
A[3.0, 1.5, 4.0, 1.5, 5.0]
B[3, 1, 4, 1, 5]
C[3.0, 2.0, 4.0, 2.0, 5.0]
D[3, 2, 4, 2, 5]
Attempts:
2 left
💡 Hint
The default method is 'average', which assigns the average rank to tied values.
data_output
intermediate
2:00remaining
Ranking with method='min'
Given the pandas Series below, what is the output of s.rank(method='min')?
Pandas
import pandas as pd
s = pd.Series([7, 3, 7, 1, 3])
ranks = s.rank(method='min')
print(ranks.tolist())
A[3.0, 2.0, 3.0, 1.0, 2.0]
B[4.0, 2.0, 4.0, 1.0, 2.0]
C[4, 2, 4, 1, 2]
D[4.0, 3.0, 4.0, 1.0, 3.0]
Attempts:
2 left
💡 Hint
The 'min' method assigns the lowest rank to all tied values.
visualization
advanced
2:00remaining
Visualizing different ranking methods
Which ranking method produces the following ranks for the Series [10, 20, 20, 30]? Ranks: [1.0, 3.0, 3.0, 4.0]
Pandas
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([10, 20, 20, 30])
methods = ['average', 'min', 'max', 'dense']
ranks = {m: s.rank(method=m).tolist() for m in methods}
print(ranks)
Adense
Bmin
Caverage
Dmax
Attempts:
2 left
💡 Hint
The 'max' method assigns the highest rank to tied values.
🧠 Conceptual
advanced
2:00remaining
Understanding 'dense' ranking method
Which statement correctly describes the 'dense' ranking method in pandas rank()?
A'dense' ranking assigns ranks randomly to tied values.
B'dense' ranking assigns average ranks to tied values, possibly creating gaps.
C'dense' ranking assigns ranks with no gaps, incrementing by 1 for each distinct value.
D'dense' ranking assigns the minimum rank to tied values, skipping ranks after ties.
Attempts:
2 left
💡 Hint
Think about how 'dense' differs from 'min' and 'max' in rank numbering.
🔧 Debug
expert
2:00remaining
Identify the error in rank() usage
What error will the following code raise when executed? import pandas as pd s = pd.Series([1, 2, 3]) ranks = s.rank(method='median') print(ranks.tolist())
Pandas
import pandas as pd
s = pd.Series([1, 2, 3])
ranks = s.rank(method='median')
print(ranks.tolist())
AValueError: method must be one of ['average', 'min', 'max', 'first', 'dense']
BTypeError: unsupported method argument
CAttributeError: 'Series' object has no attribute 'rank'
DNo error, output: [1.0, 2.0, 3.0]
Attempts:
2 left
💡 Hint
Check the allowed values for the 'method' parameter in pandas rank().