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.
astype() for type conversion in 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.
df['age'] = df['age'].astype(int)
df['price'] = df['price'].astype(float)
df = df.astype({'age': 'int', 'name': 'str'})pd.to_datetime() instead of astype().df['date'] = pd.to_datetime(df['date'])
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.
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)
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.
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.