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
Change Data Type - DataFrame Conversion INPUT DataFrame student_id name age grade 1 Alice 20 85.0 2 Bob 21 92.0 Column Types: student_id: int name: object age: int grade: float Problem: grade is float, needs to be int ALGORITHM STEPS 1 Access DataFrame Get the 'grade' column 2 Use astype() Convert float to int 3 Reassign Column Update grade in place 4 Return DataFrame Modified students df students['grade'] = students['grade'] .astype(int) return students 85.0 --> 85 FINAL RESULT student_id name age grade 1 Alice 20 85 2 Bob 21 92 Updated Types: student_id: int name: object age: int grade: int OK - Conversion Successful! Output: [{..., "grade": 85}, {..., "grade": 92}] Key Insight: The astype() method in Pandas efficiently converts column data types. When converting float to int, decimal values are truncated (not rounded). Use students['grade'].astype(int) for direct conversion. This modifies the column in place when reassigned. Time Complexity: O(n) where n = number of rows. TutorialsPoint - Change Data Type | Pandas astype() Method
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