Challenge - 5 Problems
nlargest_nsmallest_master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nlargest() on a DataFrame column
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'A': [5, 2, 9, 1, 7], 'B': [10, 20, 30, 40, 50]}) result = df['A'].nlargest(3) print(result)
Attempts:
2 left
💡 Hint
nlargest() returns the top n values sorted descending by default.
✗ Incorrect
The nlargest(3) method returns the top 3 largest values from column 'A' sorted in descending order, preserving the original index.
❓ data_output
intermediate2:00remaining
Result of nsmallest() on multiple columns
Given the DataFrame below, what is the result of selecting the 2 rows with the smallest values in column 'B'?
Pandas
import pandas as pd df = pd.DataFrame({'A': [3, 1, 4, 2], 'B': [40, 10, 20, 30]}) result = df.nsmallest(2, 'B') print(result)
Attempts:
2 left
💡 Hint
nsmallest() returns rows with smallest values in the specified column, sorted ascending.
✗ Incorrect
The nsmallest(2, 'B') method returns the two rows with the smallest values in column 'B', sorted ascending by 'B'.
❓ visualization
advanced3:00remaining
Visualizing top 3 largest values using nlargest()
Which option correctly plots the top 3 largest values from column 'score' in descending order using pandas and matplotlib?
Pandas
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'name': ['Anna', 'Bob', 'Cara', 'Dan', 'Eli'], 'score': [88, 92, 79, 95, 85]}) top_scores = df.nlargest(3, 'score') plt.bar(top_scores['name'], top_scores['score']) plt.show()
Attempts:
2 left
💡 Hint
nlargest() returns rows sorted descending by the column specified.
✗ Incorrect
The nlargest(3, 'score') returns the top 3 rows sorted descending by 'score'. The plot shows bars in that order from left to right.
🔧 Debug
advanced2:00remaining
Identify the error in nsmallest() usage
What error will this code raise and why?
Pandas
import pandas as pd df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}) result = df.nsmallest(2, ['X', 'Z']) print(result)
Attempts:
2 left
💡 Hint
Check if all columns specified exist in the DataFrame.
✗ Incorrect
The column 'Z' does not exist in the DataFrame, so pandas raises a KeyError.
🚀 Application
expert3:00remaining
Using nlargest() to find top scoring students with ties
Given the DataFrame below, which option correctly returns the top 3 students by score including ties (students with the same score as the 3rd highest)?
Pandas
import pandas as pd df = pd.DataFrame({'student': ['Amy', 'Ben', 'Cara', 'Dan', 'Eli'], 'score': [90, 85, 90, 80, 85]}) # Your task: get top 3 scores including ties # Which code achieves this?
Attempts:
2 left
💡 Hint
nlargest(3) returns exactly 3 rows, but may exclude ties with the same score as the 3rd highest.
✗ Incorrect
Option B finds the 3rd highest score, then selects all students with scores greater or equal to that, including ties.