Python astype() | Type Conversion of Data Columns

In programming, data is almost always required to complete simple to complex tasks. However, it is not always in a valid format or data type. Python provides various functions to convert data types into valid ones based on the requirement.

To make data more readable, usable, and efficient, we need to convert the data type of a Pandas DataFrame column by using the “astype()” method.

This post provides an in-depth guide on converting data types of any given data column using the “astype()” function.

The contents are as follows:

What is Python astype() Function?

In pandas, the “astype()” function is utilized to cast objects to a specified/particular data type. It can be used to cast a specific column data type to another data type or to cast the entire pandas object to the same type. The syntax of the Python “astype()” function is shown below:

DataFrame.astype(dtype, copy=True, errors='raise')

In the above syntax: 

  • The “dtype” parameter is used to convert the complete pandas object or columns to the same/identical type. 
  • The other parameter “copy=True” retrieves a copy of the object. 
  • The third parameter “errors” is used to handle an error.

Here are some examples:

Example 1: Converting the Data Type of Single DataFrame Columns

The following example is used to convert the data type of a single column of the given DataFrame:

Code:

import pandas as pd
data = pd.DataFrame({"Name":['John', 'Anna', 'Joseph'], "Age": [22, 32, 21]})
print("Original Data frame:\n")
print(data)
#converting the datatype of the 'Age' column
data['Age'] = data['Age'].astype('float')
print('\n', data)
  • The module named “pandas” is imported.
  • The “pd.DataFrame()” function creates the DataFrame by taking the dictionary value as a parameter.
  • The “astype()” function is used to convert the integer type of column named “Age” to float type.

Output:

The column name “integer” type is converted into float type.

Example 2: Converting Data Type of Multiple DataFrame Columns 

The below example is used to convert the data type of multiple columns of DataFrame:

Code:

import pandas as pd
data = pd.DataFrame({"Name":['John', 'Anna', 'Joseph'], "Age": [22, 32, 21],
                    "Salary": [1300, 1500, 1700]})
print("Original Data frame:\n")
print(data)
# convert the datatype of the 'Age' and ‘Salary’ column
data[['Age', 'Salary']] = data[['Age', 'Salary']].astype('float')
print('\n', data)
  • The module named “pandas” is imported.
  • The “pd.DataFrame()” function creates the dataframe from the dictionary.
  • The “astype()” function converts the integer type of multiple columns “Age and Salary” into float type.

Output:

The integer type of multiple columns has been converted into float type.

Example 3: Converting Data Type of Complete DataFrame Columns

The below example is used to convert the Data Type of complete DataFrame columns:

Code:

import pandas
data = {"Total Marks": [100, 100, 100, 100],
        "Obtain Marks": [30.25, 45.5, 45.75, 95.5],
        "Average Marks": [40.5, 49.75, 34.0, 20.25]}
data_frame = pandas.DataFrame(data)
print('Original DataFrame: \n', data_frame)
newdata_frame = data_frame.astype('int64')
print('\n', newdata_frame)

The “astype()” function takes the data type “int64” as an argument and converts the complete data frame column into integers.

Output:

A type conversion has been performed on data columns.

Conclusion

In Pandas, the “astype()” function is utilized to cast single, multiple, or all columns of given data to a particular data type. The “astype()” function takes various data types such as int64, float64, etc to convert data types of single or multiple columns of  DataFrame. This post explained an in-depth guide on how to convert the datatype of multiple columns.