Challenge - 5 Problems
Rank Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
The default method is 'average', which assigns the average rank to tied values.
✗ Incorrect
The rank() method assigns ranks starting at 1. The tied values (1 and 1) get the average rank (1 and 2 average to 1.5). Other values get their natural ranks.
❓ data_output
intermediate2: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())
Attempts:
2 left
💡 Hint
The 'min' method assigns the lowest rank to all tied values.
✗ Incorrect
For tied values, 'min' assigns the smallest rank among them. The two 7's get rank 4, the two 3's get rank 2, and 1 gets rank 1.
❓ visualization
advanced2: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)
Attempts:
2 left
💡 Hint
The 'max' method assigns the highest rank to tied values.
✗ Incorrect
The 'max' method assigns the maximum rank to tied values. For the two 20's, ranks are 3 and 3, so output is [1.0, 3.0, 3.0, 4.0]. The given ranks match the 'max' method.
🧠 Conceptual
advanced2:00remaining
Understanding 'dense' ranking method
Which statement correctly describes the 'dense' ranking method in pandas rank()?
Attempts:
2 left
💡 Hint
Think about how 'dense' differs from 'min' and 'max' in rank numbering.
✗ Incorrect
'Dense' ranking assigns ranks starting at 1 and increments by 1 for each distinct value without gaps, even if there are ties.
🔧 Debug
expert2: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())
Attempts:
2 left
💡 Hint
Check the allowed values for the 'method' parameter in pandas rank().
✗ Incorrect
The 'median' method is not a valid option for pandas Series.rank(). It raises a ValueError listing valid methods.