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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

1
2
3
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.