problemeasyalgorithmsleetcode-2879leetcode 2879leetcode2879

Display the First Three Rows

EasyUpdated: Aug 2, 2025
Practice on:

Problem

DataFrame: employees +-------------+--------+ | Column Name | Type | +-------------+--------+ | employee_id | int | | name | object | | department | object | | salary | int | +-------------+--------+

Write a solution to display the first3 rows of this DataFrame.

Examples

Example 1

Input: DataFrame employees
+-------------+-----------+-----------------------+--------+
| employee_id | name      | department            | salary |
+-------------+-----------+-----------------------+--------+
| 3           | Bob       | Operations            | 48675  |
| 90          | Alice     | Sales                 | 11096  |
| 9           | Tatiana   | Engineering           | 33805  |
| 60          | Annabelle | InformationTechnology | 37678  |
| 49          | Jonathan  | HumanResources        | 23793  |
| 43          | Khaled    | Administration        | 40454  |
+-------------+-----------+-----------------------+--------+
Output:
+-------------+---------+-------------+--------+
| employee_id | name    | department  | salary |
+-------------+---------+-------------+--------+
| 3           | Bob     | Operations  | 48675  |
| 90          | Alice   | Sales       | 11096  |
| 9           | Tatiana | Engineering | 33805  |
+-------------+---------+-------------+--------+
Explanation: 
Only the first 3 rows are displayed.

## Solution

### Method 1 – DataFrame Head

#### Intuition

To display the first three rows of a DataFrame, we use the `head()` method in pandas, which returns the first n rows.

#### Approach

1. Use the `head(3)` method on the DataFrame to get the first three rows.
2. Return or display the resulting DataFrame.

#### Code

{{< code_tabs >}}
##### Python

```python
class Solution:
    def display_first_three_rows(self, employees):
        return employees.head(3)

Complexity

  • ⏰ Time complexity: O(1), as only the first three rows are accessed.
  • 🧺 Space complexity: O(1), as no extra space is used beyond the output.

Comments