Display the First Three Rows - Problem

You are given a DataFrame named employees with the following structure:

Column NameType
employee_idint
nameobject
departmentobject
salaryint

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

Note: This is a Pandas DataFrame operation problem.

Input & Output

Example 1 — Basic Employee Data
$ Input: employees = [{"employee_id": 1, "name": "John", "department": "Engineering", "salary": 75000}, {"employee_id": 2, "name": "Sarah", "department": "Marketing", "salary": 65000}, {"employee_id": 3, "name": "Mike", "department": "Sales", "salary": 55000}, {"employee_id": 4, "name": "Lisa", "department": "HR", "salary": 60000}]
Output: [{"employee_id": 1, "name": "John", "department": "Engineering", "salary": 75000}, {"employee_id": 2, "name": "Sarah", "department": "Marketing", "salary": 65000}, {"employee_id": 3, "name": "Mike", "department": "Sales", "salary": 55000}]
💡 Note: The first 3 rows are returned: John (Engineering), Sarah (Marketing), and Mike (Sales)
Example 2 — Exactly 3 Rows
$ Input: employees = [{"employee_id": 10, "name": "Alice", "department": "IT", "salary": 80000}, {"employee_id": 20, "name": "Bob", "department": "Finance", "salary": 70000}, {"employee_id": 30, "name": "Carol", "department": "Operations", "salary": 62000}]
Output: [{"employee_id": 10, "name": "Alice", "department": "IT", "salary": 80000}, {"employee_id": 20, "name": "Bob", "department": "Finance", "salary": 70000}, {"employee_id": 30, "name": "Carol", "department": "Operations", "salary": 62000}]
💡 Note: When DataFrame has exactly 3 rows, all rows are returned
Example 3 — Less than 3 Rows
$ Input: employees = [{"employee_id": 100, "name": "David", "department": "Research", "salary": 85000}, {"employee_id": 101, "name": "Eve", "department": "Design", "salary": 72000}]
Output: [{"employee_id": 100, "name": "David", "department": "Research", "salary": 85000}, {"employee_id": 101, "name": "Eve", "department": "Design", "salary": 72000}]
💡 Note: When DataFrame has less than 3 rows, all available rows are returned

Constraints

  • DataFrame contains at least 0 rows
  • Each row has employee_id, name, department, and salary columns
  • employee_id is a positive integer
  • salary is a non-negative integer

Visualization

Tap to expand
Display First Three Rows: DataFrame Head OperationInput DataFrameID|Name|Dept|Salary1|John|Eng|750002|Sarah|Mar|650003|Mike|Sales|550004|Lisa|HR|600005|Tom|IT|700005 total rowshead(3)Output ResultID|Name|Dept|Salary1|John|Eng|750002|Sarah|Mar|650003|Mike|Sales|55000First 3 rows only✅ Efficient: head(3) returns exactly what we need
Understanding the Visualization
1
Input DataFrame
Employee data with multiple rows and columns
2
Apply head(3)
Use Pandas head() method to get first 3 rows
3
Output
DataFrame containing only the first 3 rows
Key Takeaway
🎯 Key Insight: Use df.head(3) - it's the standard, readable way to get the first N rows in Pandas
Asked in
Google 25 Amazon 20 Microsoft 18 Meta 15
22.3K Views
High Frequency
~2 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen