import pandas as pd
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
2886. Change Data Type
DataFrame students
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| student_id | int |
| name | object |
| age | int |
| grade | float |
+-------------+--------+
Write a solution to correct the errors:
The grade
column is stored as floats, convert it to integers.
The result format is in the following example.
Example 1: Input: DataFrame students: +------------+------+-----+-------+ | student_id | name | age | grade | +------------+------+-----+-------+ | 1 | Ava | 6 | 73.0 | | 2 | Kate | 15 | 87.0 | +------------+------+-----+-------+ Output: +------------+------+-----+-------+ | student_id | name | age | grade | +------------+------+-----+-------+ | 1 | Ava | 6 | 73 | | 2 | Kate | 15 | 87 | +------------+------+-----+-------+ Explanation: The data types of the column grade is converted to int.
原站题解
pythondata 解法, 执行用时: 244 ms, 内存消耗: 59.1 MB, 提交时间: 2023-10-07 10:38:03
''' 浮点数转整型 ''' import pandas as pd def changeDatatype(students: pd.DataFrame) -> pd.DataFrame: students['grade'] = students['grade'].apply(int) return students