跳至主要內容

2885. 重命名列


2885. 重命名列

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

题目

DataFrame students

+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| id          | int    |
| first       | object |
| last        | object |
| age         | int    |
+-------------+--------+

Write a solution to rename the columns as follows:

  • id to student_id
  • first to first_name
  • last to last_name
  • age to age_in_years

The result format is in the following example.

Example 1:

Input:

+----+---------+----------+-----+
| id | first   | last     | age |
+----+---------+----------+-----+
| 1  | Mason   | King     | 6   |
| 2  | Ava     | Wright   | 7   |
| 3  | Taylor  | Hall     | 16  |
| 4  | Georgia | Thompson | 18  |
| 5  | Thomas  | Moore    | 10  |
+----+---------+----------+-----+

Output:

+------------+------------+-----------+--------------+
| student_id | first_name | last_name | age_in_years |
+------------+------------+-----------+--------------+
| 1          | Mason      | King      | 6            |
| 2          | Ava        | Wright    | 7            |
| 3          | Taylor     | Hall      | 16           |
| 4          | Georgia    | Thompson  | 18           |
| 5          | Thomas     | Moore     | 10           |
+------------+------------+-----------+--------------+

Explanation:

The column names are changed accordingly.

题目大意

DataFrame students

+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| id          | int    |
| first       | object |
| last        | object |
| age         | int    |
+-------------+--------+

编写一个解决方案,按以下方式重命名列:

  • id 重命名为 student_id
  • first 重命名为 first_name
  • last 重命名为 last_name
  • age 重命名为 age_in_years

返回结果格式如下示例所示。

示例 1:

输入:

+----+---------+----------+-----+
| id | first   | last     | age |
+----+---------+----------+-----+
| 1  | Mason   | King     | 6   |
| 2  | Ava     | Wright   | 7   |
| 3  | Taylor  | Hall     | 16  |
| 4  | Georgia | Thompson | 18  |
| 5  | Thomas  | Moore    | 10  |
+----+---------+----------+-----+

输出:

+------------+------------+-----------+--------------+
| student_id | first_name | last_name | age_in_years |
+------------+------------+-----------+--------------+
| 1          | Mason      | King      | 6            |
| 2          | Ava        | Wright    | 7            |
| 3          | Taylor     | Hall      | 16           |
| 4          | Georgia    | Thompson  | 18           |
| 5          | Thomas     | Moore     | 10           |
+------------+------------+-----------+--------------+

解释:

列名已相应更换。

解题思路

  • 使用 Pandas 的 rename() 方法,可以通过传入一个字典来指定旧列名与新列名的映射关系。
  • 需要使用 columns 参数传入该字典,键是旧列名,值是新列名。
  • rename() 方法默认返回的是一个新的 DataFrame,可以设置 inplace=True,原地修改 DataFrame。
  • 返回修改后的 DataFrame,其中列名已更新。

复杂度分析

  • 时间复杂度O(1)rename() 方法只修改列名,是常数时间复杂度。
  • 空间复杂度O(1),通过设置 inplace=True,修改在原 DataFrame 中进行,没有创建新的 DataFrame。

代码

import pandas as pd

def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
  return students.rename(columns={'id':'student_id', 'first':'first_name', 'last':'last_name', 'age':'age_in_years'}, inplace=True)