Pandas DataFrame.astype()-Python
Last Updated :
26 Jun, 2025
DataFrame.astype() function in pandas cast a pandas object such as a DataFrame or Series to a specified data type. This is especially useful when you need to ensure that columns have the correct type, such as converting strings to integers or floats to strings.
For example:
Python
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3.5, 4.5]})
res = df.astype('string')
print(res)
print(res.dtypes)
Output A B
0 1 3.5
1 2 4.5
A string[python]
B string[python]
dtype: object
Explanation: astype('string') converts all DataFrame columns to string type, ensuring that all values, whether integers or floats, are treated as strings.
Syntax
DataFrame.astype(dtype, copy=True, errors='raise')
Parameters:
- dtype: Target data type and it can be a single type or a dict of column names to types.
- copy: If True (default), returns a copy and if False, changes in-place if possible.
- error: How to handle invalid data: 'raise' (default) or 'ignore'.
Returns: A new DataFrame or Series with updated data types.
Examples
Example 1: In this, we convert column 'A' to integer, while column 'B' remains unchanged.
Python
import pandas as pd
df = pd.DataFrame({'A': ['1', '2'], 'B': ['3.0', '4.0']})
res = df.astype({'A': int})
print(res)
print(res.dtypes)
Output A B
0 1 3.0
1 2 4.0
A int64
B object
dtype: object
Explanation: astype() method is applied to convert column 'A' from string to integer type. Column 'B' is left out, so it remains as object (string).
Example 2: In this, we convert column 'A' to integer and column 'B' to float.
Python
import pandas as pd
df = pd.DataFrame({'A': ['1', '2'], 'B': ['3.5', '4.5']})
res = df.astype({'A': int, 'B': float})
print(res)
print(res.dtypes)
Output A B
0 1 3.5
1 2 4.5
A int64
B float64
dtype: object
Explanation: astype() method is used with a dictionary to convert 'A' to integer and 'B' to float. Both columns are now in numeric form, suitable for calculations.
Example 3: In this, we try to convert column 'A' to integer, but due to a non-numeric value, the conversion is skipped and original types are retained.
Python
import pandas as pd
df = pd.DataFrame({'A': ['1', 'two'], 'B': ['3.0', '4.5']})
res = df.astype({'A': int}, errors='ignore')
print(res)
print(res.dtypes)
Output A B
0 1 3.0
1 two 4.5
A object
B object
dtype: object
Explanation: astype() method tries to convert column 'A' to integer, but the value 'two' is not numeric. Since errors='ignore' is used, pandas skips the conversion and retains the original data types.
Example 4:
Python
import pandas as pd
df = pd.DataFrame({'A': ['1.1', '2.2'], 'B': ['3', '4']})
res = df.astype({'A': float}, copy=False)
print(res)
print(res.dtypes)
Output A B
0 1.1 3
1 2.2 4
A float64
B object
dtype: object
Example 4: In this, we convert column 'A' to float with copy=False to avoid creating a new object.
Python
import pandas as pd
df = pd.DataFrame({'A': ['1.1', '2.2'], 'B': ['3', '4']})
res = df.astype({'A': float}, copy=False)
print(res)
print(res.dtypes)
Output A B
0 1.1 3
1 2.2 4
A float64
B object
dtype: object
Explanation: By setting copy=False, pandas attempts to perform the conversion without creating a new object. Column 'B' remains unchanged as object.
Related articles:
Similar Reads
Python | Pandas Dataframe.rename() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas rename() method is used to rename any index, column or row. Renaming of column
3 min read
Pandas DataFrame.where()-Python DataFrame.where() function replace values in a DataFrame based on a condition. It allows you to keep the original value where a condition is True and replace it with something else e.g., NaN or a custom value where the condition is False. For Example:Pythonimport pandas as pd import numpy as np df =
2 min read
Python | Delete rows/columns from DataFrame using Pandas.drop() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages which makes importing and analyzing data much easier. In this article, we will how to delete a row in Excel using Pandas as well as delete
4 min read
Pandas dataframe.groupby() Method Pandas groupby() function is a powerful tool used to split a DataFrame into groups based on one or more columns, allowing for efficient data analysis and aggregation. It follows a "split-apply-combine" strategy, where data is divided into groups, a function is applied to each group, and the results
6 min read
Pandas DataFrame corr() Method Pandas dataframe.corr() is used to find the pairwise correlation of all columns in the Pandas Dataframe in Python. Any NaN values are automatically excluded. To ignore any non-numeric values, use the parameter numeric_only = True. In this article, we will learn about DataFrame.corr() method in Pytho
4 min read
Pandas query() Method Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Analyzing data requires a lot of filtering operations. Pandas Dataframe provide many
2 min read
Pandas dataframe.insert()-Python DataFrame.insert() function in pandas inserts a new column into a DataFrame at a specified position. It allows you to specify the column index, column label and values to insert. This is particularly useful when you want to place a new column in a specific position instead of just appending it at th
4 min read
Pandas dataframe.sum() DataFrame.sum() function in Pandas allows users to compute the sum of values along a specified axis. It can be used to sum values along either the index (rows) or columns, while also providing flexibility in handling missing (NaN) values. Example:Pythonimport pandas as pd data = { 'A': [1, 2, 3], 'B
4 min read
Pandas DataFrame mean() Method Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DataFrame mean()Â Pandas dataframe.mean() function returns the mean of the value
2 min read
Python | Pandas dataframe.median() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.median() function return the median of the values for the requested a
2 min read