Rename Columns — LeetCode 2885 Python Solution

EasyPandas
Problem
#2885
Reading time
3 min

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 students

Complexity

MeasureComplexity
TimeO(n)
SpaceO(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.

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.

Get Stealth Interview