Challenge - 5 Problems
Sorting and Ranking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of sorting a DataFrame by a column?
Given the DataFrame below, what will be the order of the 'Name' column after sorting by 'Score' in ascending order?
Pandas
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Score': [88, 92, 85, 90] }) sorted_df = df.sort_values(by='Score') print(sorted_df['Name'].tolist())
Attempts:
2 left
💡 Hint
Think about sorting numbers from smallest to largest and how that affects the order of names.
✗ Incorrect
Sorting by 'Score' in ascending order arranges the rows from lowest to highest score. The scores are 85 (Charlie), 88 (Alice), 90 (David), and 92 (Bob), so the names follow that order.
❓ data_output
intermediate2:00remaining
What is the rank of each score in the DataFrame?
Using the same DataFrame, what is the rank of each 'Score' when ranked in ascending order with method='min'?
Pandas
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Score': [88, 92, 85, 90] }) df['Rank'] = df['Score'].rank(method='min') print(df[['Name', 'Rank']].sort_values('Rank').to_dict(orient='records'))
Attempts:
2 left
💡 Hint
Ranking assigns 1 to the smallest score, 2 to the next smallest, and so on.
✗ Incorrect
The rank method='min' assigns the smallest rank to the lowest score. Scores 85, 88, 90, 92 get ranks 1, 2, 3, 4 respectively.
🔧 Debug
advanced2:00remaining
Why does this sorting code raise an error?
Consider this code snippet. Why does it raise an error?
Pandas
import pandas as pd df = pd.DataFrame({'A': [3, 1, 2]}) sorted_df = df.sort_values('B')
Attempts:
2 left
💡 Hint
Check if the column you want to sort by exists in the DataFrame.
✗ Incorrect
The DataFrame only has column 'A'. Trying to sort by 'B' causes a KeyError because 'B' is missing.
🚀 Application
advanced2:00remaining
How to find the top 2 scores and their names?
Given the DataFrame below, which code snippet correctly finds the top 2 scores and their corresponding names?
Pandas
import pandas as pd df = pd.DataFrame({ 'Name': ['Anna', 'Brian', 'Cathy', 'Derek'], 'Score': [75, 85, 95, 80] })
Attempts:
2 left
💡 Hint
Use a method that directly selects the largest values.
✗ Incorrect
nlargest(2, 'Score') returns the rows with the two highest scores. Other options either select lowest scores or wrong rows.
🧠 Conceptual
expert2:00remaining
Why is ranking important in data analysis?
Which of the following best explains why ranking data is useful in data science?
Attempts:
2 left
💡 Hint
Think about how knowing the order of values helps in understanding data.
✗ Incorrect
Ranking assigns positions to data points based on their values, helping to compare and prioritize them effectively.