Rename Columns — LeetCode 2885 Python Solution
EasyPandas
- Problem
- #2885
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
SQL
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
SQL
DataFrame students
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| id | int |
| first | object |
| last | object |
| age | int |
+-------------+--------+Python solution
Python
import pandas as pd
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
students.rename(
columns={
'id': 'student_id',
'first': 'first_name',
'last': 'last_name',
'age': 'age_in_years',
},
inplace=True,
)
return studentsComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2885. Rename Columns?
- LeetCode 2885. Rename Columns is rated Easy on LeetCode.
- What topics does LeetCode 2885. Rename Columns cover?
- LeetCode 2885. Rename Columns is tagged Pandas on LeetCode.