跳至主要內容

2879. 显示前三行


2879. 显示前三行

🟢   🔗 力扣open in new window LeetCodeopen in new window

题目

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.

Example 1:

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.

题目大意

DataFrame: employees

+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| employee_id | int    |
| name        | object |
| department  | object |
| salary      | int    |
+-------------+--------+

编写一个解决方案,显示这个 DataFrame 的前 3 行。

示例 1:

输入: 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  |
+-------------+-----------+-----------------------+--------+

输出:

+-------------+---------+-------------+--------+
| employee_id | name    | department  | salary |
+-------------+---------+-------------+--------+
| 3           | Bob     | Operations  | 48675  |
| 90          | Alice   | Sales       | 11096  |
| 9           | Tatiana | Engineering | 33805  |
+-------------+---------+-------------+--------+

解释:

只有前 3 行被显示。

解题思路

有三种方法:

  1. 切片操作 (employees[0:3]):

    • 直接利用 Python 的切片语法,从索引 0 开始,提取到索引 3(不包含 3)。
    • 如果输入数据不足 3 行,切片会自动返回现有的所有行,无需额外处理。
  2. head() 方法 (employees.head(3)):

    • Pandas 提供的专用方法,直接返回前 n 行(此处为 3 行)。
    • 如果输入数据不足 3 行,方法会安全地返回所有现有行。
  3. iloc 方法 (employees.iloc[:3]):

    • 使用 Pandas 的索引定位方法,支持基于位置的切片操作。
    • 提取索引位置 03(不包含 3)的行。

复杂度分析

  • 时间复杂度O(1),切片、head()iloc 方法的复杂度均为 O(1),因为 Pandas 的底层实现会直接定位行,不需要逐行遍历。
  • 空间复杂度O(1),返回的子 DataFrame 引用了原始数据,因此空间复杂度为 O(1)(不创建新的数据副本)。

代码

import pandas as pd

def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:
  return employees[0:3]
  # OR
  return employees.head(3)
  # OR
  return employees.iloc[:3]