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 NameType
employee_idint
nameobject
departmentobject
salaryint

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
DataFrame First 3 Rows Selection๐Ÿ“Š Original Employee DataFrameID | Name | Department | Salary1 | John | Engineering | $75,0002 | Sarah | Marketing | $65,0003 | Mike | Sales | $55,0004 | Lisa | HR | $60,0005 | David | IT | $70,0006 | Anna | Finance | $72,000... more rows ...โœ… Result DataFrame (First 3)ID | Name | Department | Salary1 | John | Engineering | $75,0002 | Sarah | Marketing | $65,0003 | Mike | Sales | $55,000โœจ Perfect! Exactly 3 rowshead(3)๐ŸŽฏ Key Benefits:โ€ข O(1) Time Complexity - No full table scan neededโ€ข Memory Efficient - Creates view of only needed rowsโ€ข Standard Practice - Most readable and maintainable
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Creating a new DataFrame with fixed 3 rows

n
2n
โœ“ 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
Asked in
Netflix 25 Spotify 20 Airbnb 18 Uber 15
42.0K Views
Very High Frequency
~2 min Avg. Time
1.6K 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