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 the First Three Rows INPUT DataFrame: employees id name department salary 1 John Engineering 75000 2 Sarah Marketing 65000 3 Mike Sales 55000 4 Lisa HR 60000 Total: 4 rows Input as List of Dicts: [ {"id":1, "name":"John"...}, {"id":2, "name":"Sarah"...}, {"id":3, "name":"Mike"...}, {"id":4, "name":"Lisa"...} ] ALGORITHM STEPS 1 Import Pandas import pandas as pd 2 Create DataFrame df = pd.DataFrame(employees) 3 Use head() Method df.head(3) 4 Return Result First 3 rows extracted head(n) Method 1 2 3 4 Selected (n=3) Excluded FINAL RESULT id name department salary 1 John Engineering 75000 2 Sarah Marketing 65000 3 Mike Sales 55000 OK - 3 Rows Output Array: [ {"employee_id": 1, "name": "John", ...}, {"employee_id": 2, "name": "Sarah", ...}, {"employee_id": 3, "name": "Mike", ...} ] Key Insight: The pandas head(n) method returns the first n rows of a DataFrame. By default, n=5. Using head(3) efficiently extracts exactly the first 3 rows without loading the entire dataset. Time Complexity: O(n) where n is the number of rows requested | Space Complexity: O(n) TutorialsPoint - Display the First Three Rows | Pandas head() Method
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