2885. Rename Columns
DataFrame students
Column Name | Type |
---|---|
id | int |
first | object |
last | object |
age | int |
Instructions
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.
Example
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.
Submissions
python
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'
})
Explanation
Python (Pandas)
Submitted by @noeyislearning
import pandas as pd
: Import the pandas library to work with DataFrames.def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
: Define a function calledrenameColumns
that takes a DataFramestudents
as input and returns a DataFrame.return students.rename(columns={...})
: Rename the columns of the DataFrame using a dictionary where the keys are the original column names and the values are the new column names.