0
0
Pandasdata~10 mins

nlargest() and nsmallest() in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - nlargest() and nsmallest()
Start with DataFrame
Call nlargest(n, column)
Sort column descending
Select top n rows
Call nsmallest(n, column)
Sort column ascending
Select bottom n rows
Return subset DataFrame
Start with a DataFrame, then use nlargest() to get top n rows by a column sorted descending, or nsmallest() to get bottom n rows sorted ascending, returning those rows.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'Name': ['A', 'B', 'C', 'D'], 'Score': [88, 92, 85, 90]})

# Top 2 scores
top_rows = df.nlargest(2, 'Score')
print(top_rows)

# Bottom 2 scores
bottom_rows = df.nsmallest(2, 'Score')
print(bottom_rows)
This code creates a DataFrame with names and scores, assigns and prints the top 2 and bottom 2 rows by Score.
Execution Table
StepActionDataFrame StateResult
1Create DataFrame[{'A',88}, {'B',92}, {'C',85}, {'D',90}]DataFrame with 4 rows
2top_rows = df.nlargest(2, 'Score')Sort 'Score' descendingRows with scores 92 (B), 90 (D)
3print(top_rows)Subset DataFrameRows: B(92), D(90)
4bottom_rows = df.nsmallest(2, 'Score')Sort 'Score' ascendingRows with scores 85 (C), 88 (A)
5print(bottom_rows)Subset DataFrameRows: C(85), A(88)
6EndNo changeExecution complete
💡 All steps executed; nlargest and nsmallest returned correct subsets.
Variable Tracker
VariableStartAfter nlargestAfter nsmallestFinal
df[{'A',88}, {'B',92}, {'C',85}, {'D',90}]UnchangedUnchangedUnchanged
top_rowsN/A[{'B',92}, {'D',90}][{'B',92}, {'D',90}][{'B',92}, {'D',90}]
bottom_rowsN/AN/A[{'C',85}, {'A',88}][{'C',85}, {'A',88}]
Key Moments - 2 Insights
Why does nlargest(2, 'Score') return rows with scores 92 and 90, not 88?
Because nlargest sorts the 'Score' column in descending order and picks the top 2 rows, which are 92 and 90, as shown in execution_table step 2 and 3.
Does nsmallest change the original DataFrame?
No, nsmallest returns a new subset DataFrame without modifying the original, as shown in variable_tracker where 'df' remains unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what rows does nlargest(2, 'Score') return at step 3?
ARows with scores 92 and 90
BRows with scores 85 and 88
CAll rows sorted ascending
DRows with scores 88 and 85
💡 Hint
Check execution_table row 3 under 'Result' column.
At which step does the DataFrame get sorted ascending by 'Score'?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table row 4 'Action' column.
If we change nlargest(2, 'Score') to nlargest(3, 'Score'), what changes in variable_tracker?
Adf will be modified
Bbottom_rows will change to 3 rows
Ctop_rows will include 3 rows with highest scores
DNo change at all
💡 Hint
Refer to variable_tracker row for 'top_rows' and how nlargest affects it.
Concept Snapshot
nlargest(n, column) returns top n rows sorted descending by column.
nsmallest(n, column) returns bottom n rows sorted ascending by column.
Both return new DataFrames without changing original.
Use to quickly find highest or lowest values in data.
Full Transcript
We start with a DataFrame of names and scores. Calling nlargest(2, 'Score') sorts the scores descending and picks top 2 rows with scores 92 and 90. Calling nsmallest(2, 'Score') sorts ascending and picks bottom 2 rows with scores 85 and 88. Neither method changes the original DataFrame. The output is subsets of the original data showing highest or lowest values as requested.