0
0
Pandasdata~5 mins

head() and tail() for previewing in Pandas - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: head() and tail() for previewing
O(1)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
105
1005
10005

Pattern observation: The number of operations stays the same, fixed by the number of rows requested.

Final Time Complexity

Time Complexity: O(1)

This means the time to preview rows does not grow as the data gets bigger; it stays constant.

Common Mistake

[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.

Interview Connect

Understanding that previewing data is quick and does not depend on total size helps you explain efficient data handling in real projects.

Self-Check

What if we changed head(5) to head(n) where n grows with the data size? How would the time complexity change?