Search⌘ K
AI Features

Convert and Encode Data Types

Explore how to convert and encode various data types in pandas to enhance data handling. Understand methods like astype, to_numeric, to_datetime, apply custom functions, and convert_dtypes for efficient data transformation.

Convert data types

There are four main ways we can convert the data type of DataFrame columns:

  • We can use the astype() function to enforce a pandas dtype.

  • We can use the to_ functions like to_datetime() , to_timedelta(), and to_numeric() for manual dtype conversion.

  • We can use the apply() function to apply the custom Python function to columns.

  • We can use the convert_dtypes() function for automatic dtype inference and conversion.

Lets illustrate these different ways by using a dataset of students’ demographics and academic performance.

Python 3.10.4
# View students dataset
print(df)
print('='*55)
# Inspect data types
print(df.dtypes)

There are a series of changes we need to make to this dataset:

  • Convert student_id from float to the integer type.

  • Convert gender from object (aka string) to category type.

  • Convert enroll_year from object to integer type.

  • Convert birthdate from object to datetime type.

  • Convert has_scholarship from integer to ...