Change Data Type - Problem

You're working as a data analyst and have received a student dataset with a critical data type error! ๐Ÿ“Š

The DataFrame contains student information where the grade column is incorrectly stored as float values, but grades should be represented as integers for proper analysis and reporting.

Your task: Convert the grade column from float to integer data type while preserving all other columns unchanged.

Column NameCurrent TypeExpected Type
student_idintint โœ…
nameobjectobject โœ…
ageintint โœ…
gradefloat โŒint ๐ŸŽฏ

Note: This is a common data preprocessing step in pandas that ensures data consistency and proper analysis.

Input & Output

example_1.py โ€” Basic Grade Conversion
$ Input: DataFrame: student_id | name | age | grade 1 | Alice | 20 | 85.0 2 | Bob | 21 | 92.0 3 | Charlie | 19 | 78.0
โ€บ Output: DataFrame: student_id | name | age | grade 1 | Alice | 20 | 85 2 | Bob | 21 | 92 3 | Charlie | 19 | 78 Data types: grade column changed from float64 to int64
๐Ÿ’ก Note: All grade values (85.0, 92.0, 78.0) are successfully converted to integers (85, 92, 78) while preserving other columns
example_2.py โ€” Single Student Record
$ Input: DataFrame: student_id | name | age | grade 100 | Emma | 22 | 95.0
โ€บ Output: DataFrame: student_id | name | age | grade 100 | Emma | 22 | 95 Data types: grade column changed from float64 to int64
๐Ÿ’ก Note: Even with a single record, the conversion works correctly, changing 95.0 to 95
example_3.py โ€” Edge Case with Whole Number Floats
$ Input: DataFrame: student_id | name | age | grade 1 | Alex | 19 | 88.0 2 | Sarah | 20 | 100.0 3 | Mike | 21 | 75.0
โ€บ Output: DataFrame: student_id | name | age | grade 1 | Alex | 19 | 88 2 | Sarah | 20 | 100 3 | Mike | 21 | 75 Data types: grade column changed from float64 to int64
๐Ÿ’ก Note: All grades are whole numbers stored as floats, conversion preserves the numeric values perfectly

Visualization

Tap to expand
Data Type Conversion PipelineBEFOREGrade ColumnType: float6485.0, 92.0, 78.095.0, 88.0, 76.0astype(int)AFTERGrade ColumnType: int6485, 92, 7895, 88, 76Key Benefitsโœ… Vectorized operation (faster than loops)โœ… Preserves data integrityโœ… Memory efficientโœ… One-line solutionโœ… Built-in pandas optimizationโœ… Maintains DataFrame structure
Understanding the Visualization
1
Identify Column
Locate the 'grade' column with float64 data type
2
Apply Conversion
Use pandas astype(int) for vectorized conversion
3
Verify Result
Confirm grade column is now int64 type
Key Takeaway
๐ŸŽฏ Key Insight: Use pandas' built-in `astype()` method for efficient, vectorized data type conversions instead of manual row-by-row processing

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Must iterate through each of the n rows to convert grades individually

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only modifying existing DataFrame in-place, no additional data structures needed

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค number of students โ‰ค 104
  • All grade values are valid whole numbers stored as floats
  • Grade values range from 0.0 to 100.0
  • DataFrame structure must remain unchanged except for grade column data type
Asked in
Netflix 15 Spotify 12 Uber 10 Airbnb 8
12.4K Views
Medium Frequency
~5 min Avg. Time
456 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