Skip to content

2886. Change Data Type

DataFrame students

Column NameType
student_idint
nameobject
ageint
gradefloat

Instructions

  • 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

Input:

DataFrame students

student_idnameagegrade
1Ava673.0
2Kate1587.0

Output:

student_idnameagegrade
1Ava673
2Kate1587

Explanation:

The data types of the column grade is converted to int.

Submissions

python
import pandas as pd

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

Explanation

Python (Pandas)
Submitted by @noeyislearning
  • import pandas as pd: Import the pandas library to work with DataFrames.
  • def changeDatatype(students: pd.DataFrame) -> pd.DataFrame: Define a function that takes a DataFrame as input and returns a DataFrame.
  • students['grade'] = students['grade'].astype(int): Convert the grade column to integers using the astype method.
  • return students: Return the updated DataFrame.