Change Data Type - Problem
You are given a DataFrame called students with the following structure:
| Column Name | Type |
|---|---|
| student_id | int |
| name | object |
| age | int |
| grade | float |
The grade column is currently stored as floats, but it should be stored as integers. Write a solution to convert the grade column from float to int data type.
Return the modified DataFrame with the grade column converted to integers.
Input & Output
Example 1 — Basic Grade Conversion
$
Input:
students = [{"student_id": 1, "name": "Alice", "age": 20, "grade": 85.0}, {"student_id": 2, "name": "Bob", "age": 21, "grade": 92.0}]
›
Output:
[{"student_id": 1, "name": "Alice", "age": 20, "grade": 85}, {"student_id": 2, "name": "Bob", "age": 21, "grade": 92}]
💡 Note:
The grade column values 85.0 and 92.0 are converted to integers 85 and 92 respectively
Example 2 — Single Student
$
Input:
students = [{"student_id": 1, "name": "Carol", "age": 19, "grade": 78.0}]
›
Output:
[{"student_id": 1, "name": "Carol", "age": 19, "grade": 78}]
💡 Note:
Single row conversion: grade 78.0 becomes integer 78
Example 3 — Multiple Students
$
Input:
students = [{"student_id": 1, "name": "Dave", "age": 22, "grade": 95.0}, {"student_id": 2, "name": "Eve", "age": 20, "grade": 88.0}, {"student_id": 3, "name": "Frank", "age": 21, "grade": 91.0}]
›
Output:
[{"student_id": 1, "name": "Dave", "age": 22, "grade": 95}, {"student_id": 2, "name": "Eve", "age": 20, "grade": 88}, {"student_id": 3, "name": "Frank", "age": 21, "grade": 91}]
💡 Note:
All grades (95.0, 88.0, 91.0) are converted to their integer equivalents (95, 88, 91)
Constraints
- 1 ≤ students.length ≤ 104
- All grade values are valid floats
- Grade values are in range [0.0, 100.0]
Visualization
Tap to expand
Understanding the Visualization
1
Input DataFrame
Students data with float grades
2
Type Conversion
Convert grade column from float to int
3
Output DataFrame
Same data with integer grades
Key Takeaway
🎯 Key Insight: Use pandas astype() method for efficient column data type conversion
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code