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

Difficulty: Easy


Problem Description

Write a solution to display the first 3 rows of the given DataFrame employees.


Key Insights

  • The problem requires accessing a subset of a DataFrame.
  • We need to handle the DataFrame structure and extract the first three entries.
  • This is a straightforward task that involves indexing.

Space and Time Complexity

Time Complexity: O(1) - Accessing the first three rows is a constant time operation. Space Complexity: O(1) - We are not using additional data structures that scale with input size.


Solution

The solution involves using a DataFrame structure, typically found in data manipulation libraries like pandas in Python. The algorithm accesses the first three rows using indexing techniques provided by the DataFrame API. This is done by leveraging built-in functions that allow for row selection.


Code Solutions

import pandas as pd

# Create a DataFrame
data = {
    'employee_id': [3, 90, 9, 60, 49, 43],
    'name': ['Bob', 'Alice', 'Tatiana', 'Annabelle', 'Jonathan', 'Khaled'],
    'department': ['Operations', 'Sales', 'Engineering', 'InformationTechnology', 'HumanResources', 'Administration'],
    'salary': [48675, 11096, 33805, 37678, 23793, 40454]
}

employees = pd.DataFrame(data)

# Display the first 3 rows
first_three_rows = employees.head(3)
print(first_three_rows)
← Back to All Questions