nlargest() and nsmallest() in Pandas - Time & Space Complexity
When we use pandas functions like nlargest() and nsmallest(), we want to know how their speed changes as the data grows.
We ask: How much longer does it take to find the top or bottom values when the data size increases?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.DataFrame({
'values': range(1000000, 0, -1)
})
# Find the 5 largest values
largest = data['values'].nlargest(5)
# Find the 5 smallest values
smallest = data['values'].nsmallest(5)
This code finds the top 5 largest and smallest numbers from a million rows.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning through all data values once to compare and select top or bottom n items.
- How many times: Exactly one full pass over the data (1 million items in this case).
As the data size grows, the time to find the largest or smallest values grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons |
| 100 | About 100 comparisons |
| 1000 | About 1000 comparisons |
Pattern observation: Doubling the data roughly doubles the work needed.
Time Complexity: O(n)
This means the time to find the largest or smallest values grows linearly with the number of data points.
[X] Wrong: "Finding the top 5 values means sorting the entire data, so it takes a long time like O(n log n)."
[OK] Correct: pandas uses efficient methods that do not sort all data but only keep track of the top or bottom n items, so it only needs one pass through the data.
Understanding how nlargest() and nsmallest() work helps you explain efficient data selection in real tasks, showing you know how to handle big data smartly.
"What if we asked for the top half of the data instead of just 5 items? How would the time complexity change?"
Practice
nlargest() function in pandas do?Solution
Step 1: Understand the purpose of
Thenlargest()nlargest()function is designed to select rows with the highest values in a given column.Step 2: Compare with other options
Options B and C describe other functions or behaviors, and D is unrelated tonlargest().Final Answer:
Returns rows with the highest values in a specified column -> Option AQuick Check:
nlargest()= highest rows [OK]
- Confusing nlargest() with nsmallest()
- Thinking it sorts the whole DataFrame
- Assuming it removes duplicates
df?Solution
Step 1: Recall the correct method signature
The correct syntax isdf.nsmallest(n, column_name), wherenis the number of rows.Step 2: Match the options with the syntax
df.nsmallest(3, 'score') matches the correct syntax:df.nsmallest(3, 'score'). Options A and D usenlargestincorrectly, and C has arguments reversed.Final Answer:
df.nsmallest(3, 'score') -> Option BQuick Check:
nsmallest(n, column) = df.nsmallest(3, 'score') [OK]
- Swapping arguments order
- Using nlargest instead of nsmallest
- Passing column name before number
import pandas as pd
data = {'name': ['Anna', 'Bob', 'Cara', 'Dave'], 'age': [23, 45, 31, 22]}
df = pd.DataFrame(data)What is the output of
df.nlargest(2, 'age')?Solution
Step 1: Identify the ages in the DataFrame
The ages are Anna: 23, Bob: 45, Cara: 31, Dave: 22.Step 2: Select the 2 largest ages
The two largest ages are 45 (Bob) and 31 (Cara).Final Answer:
Rows for Bob and Cara with ages 45 and 31 -> Option CQuick Check:
nlargest(2, 'age') = Bob, Cara [OK]
- Choosing smallest instead of largest
- Mixing up row order
- Confusing names with ages
df.nsmallest('score', 5)Assuming
df is a DataFrame with a column named 'score'.Solution
Step 1: Check the method signature for nsmallest()
The correct order isnsmallest(n, columns), wherenis an integer andcolumnsis a string or list.Step 2: Identify the error in the code
The code passes the column name first and then the number, which is reversed.Final Answer:
The arguments are in the wrong order -> Option AQuick Check:
nsmallest(n, column) needs number first [OK]
- Swapping argument order
- Using wrong method name
- Passing wrong data types
df with columns ['product', 'price', 'rating']. You want to find the top 3 products with the highest rating but lowest price among those top rated. Which pandas code snippet correctly achieves this?Solution
Step 1: Understand the goal
We want top 3 products by highest rating, but among those, pick the ones with lowest price.Step 2: Analyze each option
df.nlargest(3, 'rating').nsmallest(3, 'price') chains nlargest then nsmallest but nsmallest after nlargest may reduce rows incorrectly. df.nlargest(3, 'rating').sort_values('price').head(3) sorts price after selecting top rating but may not correctly filter lowest price among top rated. df.nsmallest(3, 'price').nlargest(3, 'rating') reverses the order, which is wrong. df.sort_values(['rating', 'price'], ascending=[False, True]).head(3) sorts by rating descending and price ascending, then picks top 3, which correctly prioritizes rating then price.Final Answer:
df.sort_values(['rating', 'price'], ascending=[False, True]).head(3) -> Option DQuick Check:
Sort by rating desc, price asc, then head(3) [OK]
- Chaining nlargest and nsmallest incorrectly
- Not sorting by both columns properly
- Selecting wrong rows due to order
Ready to prove it?
Pass the test and this pattern moves unknown
