0
0
Pandasdata~20 mins

nlargest() and nsmallest() in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
nlargest_nsmallest_master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
0    5
1    2
3    1
Name: A, dtype: int64
B
4    7
2    9
0    5
Name: A, dtype: int64
C
2    9
4    7
0    5
Name: A, dtype: int64
D
3    1
1    2
0    5
Name: A, dtype: int64
Attempts:
2 left
💡 Hint
nlargest() returns the top n values sorted descending by default.
data_output
intermediate
2: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)
A
   A   B
1  1  10
2  4  20
B
   A   B
0  3  40
3  2  30
C
   A   B
3  2  30
2  4  20
D
   A   B
1  1  10
3  2  30
Attempts:
2 left
💡 Hint
nsmallest() returns rows with smallest values in the specified column, sorted ascending.
visualization
advanced
3: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()
APlots bars for Dan (95), Bob (92), Anna (88) in that order from left to right.
BPlots bars for Eli (85), Cara (79), Bob (92) in that order from left to right.
CPlots bars for Cara (79), Eli (85), Anna (88) in that order from left to right.
DPlots bars for Anna (88), Bob (92), Dan (95) in that order from left to right.
Attempts:
2 left
💡 Hint
nlargest() returns rows sorted descending by the column specified.
🔧 Debug
advanced
2: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)
ANo error, returns rows with smallest values in 'X' only.
BTypeError: nsmallest() expects a string, not a list.
CValueError: nsmallest() cannot handle multiple columns.
DKeyError: 'Z' because column 'Z' does not exist in the DataFrame.
Attempts:
2 left
💡 Hint
Check if all columns specified exist in the DataFrame.
🚀 Application
expert
3: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?
Atop_scores = df.nlargest(3, 'score')
B
threshold = df['score'].nlargest(3).min()
top_scores = df[df['score'] >= threshold]
Ctop_scores = df[df['score'] > df['score'].nlargest(3).min()]
Dtop_scores = df.sort_values('score', ascending=False).head(3)
Attempts:
2 left
💡 Hint
nlargest(3) returns exactly 3 rows, but may exclude ties with the same score as the 3rd highest.