EasyNo. 2886

Change Data Type — LeetCode 2886 Python Solution

Topics
Pandas
Reading time
2 min

Table schema

SQLTable schema
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

SQLExample
DataFrame students
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| student_id  | int    |
| name        | object |
| age         | int    |
| grade       | float  |
+-------------+--------+

Python solution

PythonSolution
import pandas as pd


def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
    students['grade'] = students['grade'].astype(int)
    return students

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Related problems
No.ProblemDifficulty
2877Create a DataFrame from ListEasy
0057Insert IntervalMedium
0192Word FrequencyMedium
0193Valid Phone NumbersEasy
0194Transpose FileMedium
0195Tenth LineEasy

Frequently asked questions

How hard is LeetCode 2886. Change Data Type?

LeetCode 2886. Change Data Type is rated Easy on LeetCode.

What topics does LeetCode 2886. Change Data Type cover?

LeetCode 2886. Change Data Type is tagged Pandas on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.