EasyNo. 2885

Rename Columns — LeetCode 2885 Python Solution

Topics
Pandas
Reading time
3 min

Table schema

SQLTable schema
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

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

Python solution

PythonSolution
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 students

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Related problems
No.ProblemDifficulty
2877Create a DataFrame from ListEasy
0057Insert IntervalMedium
0192Word FrequencyMedium
0193Valid Phone NumbersEasy
0194Transpose FileMedium
0195Tenth LineEasy

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.