1
+ """
2
+ 2886. Change Data Type
3
+ Solved
4
+ Easy
5
+ Companies
6
+ Hint
7
+
8
+ DataFrame students
9
+ +-------------+--------+
10
+ | Column Name | Type |
11
+ +-------------+--------+
12
+ | student_id | int |
13
+ | name | object |
14
+ | age | int |
15
+ | grade | float |
16
+ +-------------+--------+
17
+
18
+ Write a solution to correct the errors:
19
+
20
+ The grade column is stored as floats, convert it to integers.
21
+
22
+ The result format is in the following example.
23
+
24
+ Example 1:
25
+ Input:
26
+ DataFrame students:
27
+ +------------+------+-----+-------+
28
+ | student_id | name | age | grade |
29
+ +------------+------+-----+-------+
30
+ | 1 | Ava | 6 | 73.0 |
31
+ | 2 | Kate | 15 | 87.0 |
32
+ +------------+------+-----+-------+
33
+ Output:
34
+ +------------+------+-----+-------+
35
+ | student_id | name | age | grade |
36
+ +------------+------+-----+-------+
37
+ | 1 | Ava | 6 | 73 |
38
+ | 2 | Kate | 15 | 87 |
39
+ +------------+------+-----+-------+
40
+ Explanation:
41
+ The data types of the column grade is converted to int.
42
+ """
43
+
44
+ import pandas as pd
45
+
46
+ def changeDatatype (students : pd .DataFrame ) -> pd .DataFrame :
47
+ students ['grade' ] = students ['grade' ].astype (int )
48
+ return students
0 commit comments