DataFrame students
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| student_id | int |
| name | object |
| age | int |
| grade | float |
+-------------+--------+
Write a solution to correct the errors:
The grade column is stored as floats, convert it to integers.
The result format is in the following example.
Example 1: Input: DataFrame students: +------------+------+-----+-------+ | student_id | name | age | grade | +------------+------+-----+-------+ | 1 | Ava | 6 | 73.0 | | 2 | Kate | 15 | 87.0 | +------------+------+-----+-------+ Output: +------------+------+-----+-------+ | student_id | name | age | grade | +------------+------+-----+-------+ | 1 | Ava | 6 | 73 | | 2 | Kate | 15 | 87 | +------------+------+-----+-------+ Explanation: The data types of the column grade is converted to int.
Problem Overview: You are given a dataset where one or more fields are stored using the wrong data type (for example, numbers stored as strings). The task is to convert the column into the correct data type so it can be used for numeric operations, sorting, or analysis.
Approach 1: Using DataFrame Libraries (O(n) time, O(1) extra space)
Most modern languages provide high-level data processing libraries that handle type conversion efficiently. In Python, libraries like pandas allow direct casting using astype(). Similar functionality exists in JavaScript data libraries, Java data processing frameworks, and C# data table APIs. The key idea is simple: select the column and apply a built-in conversion function that transforms every value to the target type internally. These libraries are optimized in native code and perform the conversion in linear time relative to the number of rows.
This approach is concise and reliable because the library automatically handles iteration, parsing, and potential edge cases such as null values. When working with structured datasets, analytics pipelines, or tabular processing, this is the most practical solution.
Approach 2: Manual Iteration and Conversion (O(n) time, O(1) extra space)
If a DataFrame-style library is not available, you can convert values manually. Iterate through each record in the dataset, read the value as a string (or existing type), and apply a type conversion function such as int(), parseInt(), or language-specific casting operators. Replace the original value with the converted one while iterating through the collection.
This approach explicitly performs the conversion step for every element. The algorithm scans the dataset once, making the time complexity O(n). Memory usage stays constant because the conversion happens in place without allocating additional structures.
Manual iteration is useful when you are working with raw arrays, custom objects, or low-level systems where DataFrame utilities are unavailable. It also helps reinforce basic iteration and type conversion concepts that appear frequently in data processing tasks.
Recommended for interviews: The DataFrame approach is the cleanest solution in real-world data engineering environments because it uses optimized library operations. However, interviewers often expect you to explain the underlying logic. Showing the manual iteration approach demonstrates that you understand how each value is parsed and converted, while still recognizing that the optimal complexity remains O(n).
In this approach, we use the standard data handling libraries or frameworks provided by each language to manipulate the DataFrame. This generally involves directly converting the column data type from float to int.
This Python code uses pandas, a popular library for data manipulation. The 'astype' method is used to convert the 'grade' column to integer type, thereby eliminating the decimal part.
Python
C#
JavaScript
Java
C
Time Complexity: O(n), where n is the number of elements in the DataFrame.
Space Complexity: O(1), as it modifies the DataFrame in place.
This approach involves manually iterating over the data structure (for example, an array or list) and converting each 'grade' entry from a float or double to an integer representation. It's a more generalized approach and less dependent on specific data manipulation libraries.
This code demonstrates how to manually iterate over a list of dictionaries in Python, converting the 'grade' field of each dictionary to an integer.
Python
C
JavaScript
Time Complexity: O(n), where n is the number of dictionaries.
Space Complexity: O(1), due to in-place updates.
Python
| Approach | Complexity |
|---|---|
| Approach 1: Using DataFrame Libraries | Time Complexity: O(n), where n is the number of elements in the DataFrame. |
| Approach 2: Manual Iteration and Conversion | Time Complexity: O(n), where n is the number of dictionaries. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using DataFrame Libraries | O(n) | O(1) | Best for structured datasets and analytics workflows using libraries like pandas or similar table APIs |
| Manual Iteration and Conversion | O(n) | O(1) | Useful when working with raw arrays, objects, or environments without DataFrame utilities |
2886. Change Data Type | LeetCode | Python | Pandas • You Data And AI • 398 views views
Watch 2 more video solutions →Practice Change Data Type with our built-in code editor and test cases.
Practice on FleetCode