-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2885-RenameColumns.py
More file actions
63 lines (58 loc) · 1.89 KB
/
2885-RenameColumns.py
File metadata and controls
63 lines (58 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 2885. Rename Columns
# 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 1:
# 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.
import pandas as pd
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
students = students.rename(
columns={
"id": "student_id",
"first": "first_name",
"last": "last_name",
"age": "age_in_years",
}
)
return students
if __name__ == "__main__":
l = [
[101, 1, 15,1],
[101, 2, 11,2],
[103, 3, 11,3],
[104, 4, 20,4]
]
print(renameColumns(pd.DataFrame(l,columns=["id","first","last","age"])))