0
0
Pandasdata~5 mins

astype() for type conversion in Pandas

Choose your learning style9 modes available
Introduction

We use astype() to change the type of data in a column or series. This helps us work with data correctly, like turning numbers stored as text into real numbers.

When you have numbers stored as text and want to do math with them.
When you want to convert a column of dates stored as strings into date objects.
When you need to save memory by converting data to a smaller type, like from float64 to float32.
When you want to convert categorical text data into category type for faster processing.
When you want to ensure data types match before merging or analyzing data.
Syntax
Pandas
DataFrame['column_name'] = DataFrame['column_name'].astype(new_type)

new_type can be a Python type like int, float, str, or a NumPy type like np.int32.

You can convert multiple columns by applying astype() on the whole DataFrame with a dictionary of column names and types.

Examples
Convert the 'age' column to integers.
Pandas
df['age'] = df['age'].astype(int)
Convert the 'price' column to floats for calculations.
Pandas
df['price'] = df['price'].astype(float)
Convert multiple columns at once: 'age' to int and 'name' to string.
Pandas
df = df.astype({'age': 'int', 'name': 'str'})
Note: For dates, use pd.to_datetime() instead of astype().
Pandas
df['date'] = pd.to_datetime(df['date'])
Sample Program

This code creates a DataFrame with 'age' and 'height' as strings. Then it converts 'age' to integers and 'height' to floats. Finally, it prints the data types before and after conversion and shows the DataFrame.

Pandas
import pandas as pd

data = {'age': ['25', '30', '22'], 'height': ['5.5', '6.0', '5.8']}
df = pd.DataFrame(data)

print('Before type conversion:')
print(df.dtypes)

# Convert 'age' to int and 'height' to float
 df['age'] = df['age'].astype(int)
 df['height'] = df['height'].astype(float)

print('\nAfter type conversion:')
print(df.dtypes)

print('\nDataFrame content:')
print(df)
OutputSuccess
Important Notes

If conversion fails (like converting 'abc' to int), astype() will raise an error.

Use errors='ignore' or errors='raise' in some pandas functions, but astype() does not have this parameter.

For converting to category type, use astype('category') to save memory and speed up operations.

Summary

astype() changes the data type of columns or series in pandas.

It helps prepare data for analysis by ensuring correct types.

Always check data before converting to avoid errors.