Change Data Type - Problem

You are given a DataFrame called students with the following structure:

Column NameType
student_idint
nameobject
ageint
gradefloat

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
DataFrame Grade Type Conversion: Float → IntegerInput DataFramestudent_id | name | age | grade(float)1 | Alice | 20 | 85.02 | Bob | 21 | 92.03 | Carol | 19 | 78.04 | Dave | 22 | 95.0Floatastype(int)Convert TypeOutput DataFramestudent_id | name | age | grade(int)1 | Alice | 20 | 852 | Bob | 21 | 923 | Carol | 19 | 784 | Dave | 22 | 95IntGrade column successfully converted from float to integer data type
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
Asked in
Netflix 25 Microsoft 20
12.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen