We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Display the First Three Rows

Number: 3065

Difficulty: Easy

Paid? No

Companies: Amazon


Problem Description

Display only the first 3 rows of the given employees DataFrame.


Key Insights

  • For a DataFrame, built-in functions such as head() can be utilized to extract the first few rows.
  • In non-Pandas environments, list slicing or iterating through the first fixed number of elements achieves the same result.
  • The problem is straightforward and involves no complex operations.

Space and Time Complexity

Time Complexity: O(1) — Only three rows are accessed regardless of the total data size. Space Complexity: O(1) — No extra space is required beyond storing the reference to the three rows.


Solution

The solution involves accessing the first three rows of the DataFrame or array-like data structure. In Python, Pandas’ head(3) function directly retrieves these rows. In other programming languages, the approach typically involves slicing or iterating over the data structure for exactly three elements. Since the number to extract is fixed, the operation is constant in time and space.


Code Solutions

# Import the pandas library
import pandas as pd

# Assuming 'employees' is a pandas DataFrame
# Use the head() function to extract the first three rows
result = employees.head(3)
print(result)
← Back to All Questions