We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Change Data Type

Number: 3069

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given a DataFrame named students, convert the grade column from float values to integers by removing the decimal part, while leaving the other columns unchanged.


Key Insights

  • The DataFrame has a grade column that is represented as floats.
  • The task is to convert these float values to integers.
  • The conversion should truncate any decimal portion.

Space and Time Complexity

Time Complexity: O(n) where n is the number of rows since each row's grade is processed once. Space Complexity: O(1) as the conversion is done in-place.


Solution

The solution involves updating the grade column in the DataFrame by converting its float values to integers. Most high-level languages, like Python with pandas, allow direct type conversion using built-in functions. In this case, you can simply use the type-casting function (for example, astype(int) in pandas) to achieve the necessary conversion. This method is efficient both in terms of time and space since it operates directly on the DataFrame without needing additional data structures.


Code Solutions

import pandas as pd

# Assuming 'students' is a pandas DataFrame with columns: student_id, name, age, grade.
# Convert the 'grade' column from float to int.
students['grade'] = students['grade'].astype(int)

# Print the updated DataFrame.
print(students)
← Back to All Questions