Display the First Three Rows - Problem
Working with data analysis often requires examining samples of large datasets. In this fundamental pandas operation, you need to display the first 3 rows of an employee DataFrame.
You are given a DataFrame called employees with the following structure:
| Column Name | Type |
|---|---|
| employee_id | int |
| name | object |
| department | object |
| salary | int |
Goal: Write a pandas solution that returns only the first 3 rows of this DataFrame, maintaining all columns and original data types.
This is a common data exploration task where analysts need to quickly preview data structure and content before performing complex operations.
Input & Output
example_1.py โ Basic Employee DataFrame
$
Input:
employees = pd.DataFrame({
'employee_id': [1, 2, 3, 4, 5],
'name': ['John', 'Sarah', 'Mike', 'Lisa', 'David'],
'department': ['Engineering', 'Marketing', 'Sales', 'HR', 'IT'],
'salary': [75000, 65000, 55000, 60000, 70000]
})
โบ
Output:
employee_id name department salary
0 1 John Engineering 75000
1 2 Sarah Marketing 65000
2 3 Mike Sales 55000
๐ก Note:
Returns the first 3 rows of the DataFrame, displaying employee IDs 1, 2, and 3 with all their corresponding information.
example_2.py โ DataFrame with Exactly 3 Rows
$
Input:
employees = pd.DataFrame({
'employee_id': [101, 102, 103],
'name': ['Alice', 'Bob', 'Carol'],
'department': ['Finance', 'Operations', 'Legal'],
'salary': [80000, 75000, 85000]
})
โบ
Output:
employee_id name department salary
0 101 Alice Finance 80000
1 102 Bob Operations 75000
2 103 Carol Legal 85000
๐ก Note:
When the DataFrame has exactly 3 rows, all rows are returned since we're requesting the first 3 rows.
example_3.py โ DataFrame with Less Than 3 Rows
$
Input:
employees = pd.DataFrame({
'employee_id': [201, 202],
'name': ['Emma', 'Frank'],
'department': ['Research', 'Security'],
'salary': [90000, 65000]
})
โบ
Output:
employee_id name department salary
0 201 Emma Research 90000
1 202 Frank Security 65000
๐ก Note:
Edge case: When DataFrame has fewer than 3 rows, head(3) returns all available rows without error.
Visualization
Tap to expand
Understanding the Visualization
1
Original DataFrame
Start with a DataFrame containing employee records
2
Apply head(3)
Use pandas head() method to select first 3 rows
3
Result DataFrame
Get a new DataFrame with exactly 3 rows (or fewer if original was smaller)
Key Takeaway
๐ฏ Key Insight: Pandas head() method provides direct access to the first N rows without scanning the entire dataset, making it both efficient and intuitive for data exploration tasks.
Time & Space Complexity
Time Complexity
O(1)
Only accessing first 3 rows regardless of DataFrame size
โ Linear Growth
Space Complexity
O(1)
Creating a new DataFrame with fixed 3 rows
โ Linear Space
Constraints
- DataFrame can have 0 to 106 rows
- employee_id values are unique positive integers
- name strings are non-empty and contain only letters and spaces
- department strings represent valid business departments
- salary values are positive integers representing annual salary in dollars
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code