Pandas DataFrame Astype Method

PandasPandasBeginner
Practice Now

Introduction

In this lab, we will learn how to use the astype() method in the Pandas library in Python. The astype() method allows us to convert the datatype of a Pandas DataFrame to a specified type. We can convert the datatypes of all columns or specific columns in the DataFrame.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Convert all column datatypes of DataFrame

We can use the astype() method to convert the datatypes of all columns in a DataFrame to a specified type. Here's an example:

import pandas as pd

## Create a DataFrame
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data)

print("----Before converting datatype of DataFrame-----")
print(df.dtypes)

## Convert the datatypes of all columns to int32
df = df.astype('int32')

print("----After converting datatype of DataFrame-----")
print(df.dtypes)

Output:

----Before converting datatype of DataFrame-----
A int64
B int64
dtype: object

----After converting datatype of DataFrame-----
A int32
B int32
dtype: object

In this example, we create a DataFrame with two columns A and B. Before converting the datatypes, both columns have the int64 datatype. After using the astype() method with the argument 'int32', both columns are converted to the int32 datatype.

Convert specific column datatype of DataFrame

We can also use the astype() method to convert the datatype of a specific column in a DataFrame. Here's an example:

import pandas as pd

## Create a DataFrame
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data)

print("----Before converting datatype of DataFrame-----")
print(df.dtypes)

## Convert the datatype of column 'A' to int32
df['A'] = df['A'].astype('int32')

print("----After converting single column datatype of DataFrame-----")
print(df.dtypes)

Output:

----Before converting datatype of DataFrame-----
A int64
B int64
dtype: object

----After converting single column datatype of DataFrame-----
A int32
B int64
dtype: object

In this example, we create a DataFrame with two columns A and B. Before converting the datatype, column A has the int64 datatype and column B has the int64 datatype. After using the astype() method with the argument 'int32' on column A, only the datatype of column A is converted to int32, while the datatype of column B remains int64.

Checking the converted DataFrame

After converting the datatypes of a DataFrame, we can check the updated DataFrame to verify if the conversions were successful. Here's an example:

import pandas as pd

## Create a DataFrame
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data)

print("----After converting single column datatype of DataFrame-----")
df['B'] = df['B'].astype('float')
print(df.dtypes)
print("-----DataFrame after converting to float datatypes-----")
print(df)

Output:

----After converting single column datatype of DataFrame-----
A int64
B float64
dtype: object

-----DataFrame after converting to float datatypes-----
   A     B
0  1   6.0
1  2   7.0
2  3   8.0
3  4   9.0
4  5  10.0

In this example, we create a DataFrame with two columns A and B. After using the astype() method with the argument 'float' on column B, the datatype of column B is converted to float64. We then print the datatypes of the DataFrame to confirm the changes, and print the DataFrame itself to see the updated values.

Summary

In this lab, we learned how to use the astype() method in Pandas to convert the datatypes of a DataFrame. We saw how to convert all column datatypes and specific column datatypes using the astype() method, and also checked the resulting DataFrame after the conversions. The astype() method is useful for manipulating and transforming data in a Pandas DataFrame.

Other Pandas Tutorials you may like