欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 每日leetcode

每日leetcode

2025/6/20 13:39:51 来源:https://blog.csdn.net/XiaoyaoCarter/article/details/148717960  浏览:    关键词:每日leetcode

2879. 显示前三行 - 力扣(LeetCode)

题目

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. 使用dataframe的head()方法获取前3行。

代码实现

import pandas as pddef selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:return employees.head(3)

知识积累

  • df获取前n行:df.head(n),n缺省时默返回前5行。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词