0
0
Pandasdata~20 mins

Why sorting and ranking matter in Pandas - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting and Ranking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A['Charlie', 'Alice', 'David', 'Bob']
B['Bob', 'David', 'Alice', 'Charlie']
C['Alice', 'Bob', 'Charlie', 'David']
D['David', 'Charlie', 'Bob', 'Alice']
Attempts:
2 left
💡 Hint
Think about sorting numbers from smallest to largest and how that affects the order of names.
data_output
intermediate
2: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'))
A[{'Name': 'Bob', 'Rank': 1.0}, {'Name': 'David', 'Rank': 2.0}, {'Name': 'Alice', 'Rank': 3.0}, {'Name': 'Charlie', 'Rank': 4.0}]
B[{'Name': 'Charlie', 'Rank': 1.0}, {'Name': 'Alice', 'Rank': 2.0}, {'Name': 'David', 'Rank': 3.0}, {'Name': 'Bob', 'Rank': 4.0}]
C[{'Name': 'Alice', 'Rank': 1.0}, {'Name': 'Bob', 'Rank': 2.0}, {'Name': 'Charlie', 'Rank': 3.0}, {'Name': 'David', 'Rank': 4.0}]
D[{'Name': 'David', 'Rank': 1.0}, {'Name': 'Charlie', 'Rank': 2.0}, {'Name': 'Bob', 'Rank': 3.0}, {'Name': 'Alice', 'Rank': 4.0}]
Attempts:
2 left
💡 Hint
Ranking assigns 1 to the smallest score, 2 to the next smallest, and so on.
🔧 Debug
advanced
2: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')
AKeyError because column 'B' does not exist in the DataFrame
BTypeError because 'B' is not a valid argument type
CSyntaxError due to missing parentheses
DValueError because sorting requires numeric data
Attempts:
2 left
💡 Hint
Check if the column you want to sort by exists in the DataFrame.
🚀 Application
advanced
2: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]
})
Adf.sort_values('Score').head(2)[['Name', 'Score']]
Bdf.nsmallest(2, 'Score')[['Name', 'Score']]
Cdf.sort_values('Score', ascending=False).tail(2)[['Name', 'Score']]
Ddf.nlargest(2, 'Score')[['Name', 'Score']]
Attempts:
2 left
💡 Hint
Use a method that directly selects the largest values.
🧠 Conceptual
expert
2:00remaining
Why is ranking important in data analysis?
Which of the following best explains why ranking data is useful in data science?
ARanking removes duplicate data points to clean the dataset.
BRanking sorts data alphabetically to improve readability.
CRanking helps to identify the relative position of data points, which is useful for comparisons and decision making.
DRanking converts numerical data into categorical data for visualization.
Attempts:
2 left
💡 Hint
Think about how knowing the order of values helps in understanding data.