head() and tail() for previewing in Pandas - Time & Space Complexity
When we use head() or tail() in pandas, we want to quickly see a small part of our data.
We ask: How does the time to get this preview change as the data gets bigger?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'A': range(1000000),
'B': range(1000000, 2000000)
})
preview_top = df.head(5)
preview_bottom = df.tail(5)
This code creates a large table and then shows the first 5 and last 5 rows.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Selecting a fixed number of rows from the start or end.
- How many times: Exactly 5 rows are accessed each time, no matter the total size.
Getting 5 rows from the start or end takes the same effort whether the table has 10 or 1,000,000 rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 |
| 100 | 5 |
| 1000 | 5 |
Pattern observation: The number of operations stays the same, fixed by the number of rows requested.
Time Complexity: O(1)
This means the time to preview rows does not grow as the data gets bigger; it stays constant.
[X] Wrong: "Getting the first or last rows takes longer if the table is huge."
[OK] Correct: Because pandas directly accesses only the requested rows, it does not scan the whole table.
Understanding that previewing data is quick and does not depend on total size helps you explain efficient data handling in real projects.
What if we changed head(5) to head(n) where n grows with the data size? How would the time complexity change?
Practice
head() function do in pandas?Solution
Step 1: Understand the purpose of
Thehead()head()function is designed to display the first few rows of a DataFrame to preview data.Step 2: Compare with other options
tail()shows the last few rows, not the first. Deleting or sorting are unrelated tohead().Final Answer:
Shows the first few rows of the DataFrame -> Option CQuick Check:
head()= first rows [OK]
- Confusing head() with tail()
- Thinking head() deletes rows
- Assuming head() sorts data
df?Solution
Step 1: Recall the syntax for tail()
The correct syntax to get last rows isdf.tail(n), where n is the number of rows.Step 2: Check each option
df.tail(3)is correct.df.head(3)shows first rows.df.tail[-3]is invalid syntax.df.last(3)does not exist.Final Answer:
df.tail(3) -> Option AQuick Check:
Use tail(n) for last n rows [OK]
- Using head() instead of tail()
- Using square brackets with tail()
- Using non-existent last() method
df below:A B 0 5 10 1 6 11 2 7 12 3 8 13 4 9 14What will
df.head(2) return?Solution
Step 1: Understand what head(2) does
head(2)returns the first 2 rows of the DataFrame, which are rows with index 0 and 1.Step 2: Identify the rows in the DataFrame
Rows 0 and 1 have values (5,10) and (6,11) respectively, matching the first two rows.Final Answer:
Rows with index 0 and 1 -> Option BQuick Check:
head(2) = first 2 rows [OK]
- Selecting last rows instead of first
- Confusing index numbers
- Assuming head() returns columns
df.tail(3) but get an error: TypeError: 'int' object is not callable. What is the most likely cause?Solution
Step 1: Understand the error message
TypeError: 'int' object is not callablemeans you tried to call an integer like a function.Step 2: Identify cause related to
If you assignedtailtailas an integer variable, it overrides the method, causing this error when callingdf.tail(3).Final Answer:
You assigned a variable namedtailas an integer earlier -> Option AQuick Check:
Variable name conflicts cause callable errors [OK]
- Using square brackets instead of parentheses
- Assuming empty DataFrame causes this error
- Ignoring import errors
df with 1000 rows. You want to preview the first 5 rows and the last 5 rows together in one DataFrame. Which code correctly does this?Solution
Step 1: Understand how to combine DataFrames vertically
To combine rows from two DataFrames, usepd.concat()orappend(). The plus operator and merge are incorrect here.Step 2: Check each option
pd.concat([df.head(5), df.tail(5)])correctly stacks first and last 5 rows.df.head(5) + df.tail(5)causes error.append()is deprecated in recent pandas versions.merge()is for joining on columns, not stacking rows.Final Answer:
pd.concat([df.head(5), df.tail(5)]) -> Option DQuick Check:
Use pd.concat() to stack DataFrames [OK]
- Using + operator to add DataFrames
- Using deprecated append() method
- Using merge() instead of concat()
Ready to prove it?
Pass the test and this pattern moves unknown
