0
0
Data Analysis Pythondata~5 mins

head() and tail() in Data Analysis Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: head() and tail()
O(1)
Understanding Time Complexity

We want to understand how the time to run head() and tail() changes as the data size grows.

Specifically, how does the number of rows in a dataset affect these operations?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import pandas as pd

data = pd.DataFrame({'A': range(1000000)})

first_rows = data.head(5)
last_rows = data.tail(5)

This code gets the first 5 rows and the last 5 rows from a large dataset.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Selecting a fixed number of rows (5) from the start or end.
  • How many times: Exactly 5 rows are accessed each time, no matter the dataset size.
How Execution Grows With Input

Getting 5 rows from the start or end takes the same time whether the dataset has 10 or 1,000,000 rows.

Input Size (n)Approx. Operations
105
1005
10005

Pattern observation: The number of operations stays the same because we only look at a fixed number of rows.

Final Time Complexity

Time Complexity: O(1)

This means the time to get the first or last few rows does not grow as the dataset gets bigger.

Common Mistake

[X] Wrong: "Getting the first or last rows takes longer if the dataset is huge."

[OK] Correct: Because head() and tail() only access a small fixed number of rows, their time does not depend on the total size.

Interview Connect

Understanding how simple data access methods scale 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 dataset size? How would the time complexity change?