2885 Rename Columns
Problem Statement
DataFrame students
id
int
first
object
last
object
age
int
Write a solution to rename the columns as follows:
id
tostudent_id
first
tofirst_name
last
tolast_name
age
toage_in_years
The result format is in the following example.
For the whole problem statement, please refer here.
Plans
The DataFrame contains columns: id, first, last, and age.
We will rename the columns as follows:
id
tostudent_id
first
tofirst_name
last
tolast_name
age
toage_in_years
Use the appropriate pandas function to rename the columns.
Return the Modified DataFrame
Solution
Explanation
Import Pandas
We start by importing the Pandas library, which provides data structures and operations for manipulating numerical tables and time series.
Define the Function
We define a function
renameColumns
that takes a single argumentstudents
, which is a DataFrame containing student data.
Renaming Columns
We use the
rename
method on the DataFramestudents
to rename the columns as specified in the problem statement.The
columns
argument is a dictionary where the keys are the current column names and the values are the new column names.
Return the Result
We return the modified DataFrame with the renamed columns as specified in the problem statement.
Last updated