Pandas DataFrame astype() Method
In this tutorial, we will learn the Python pandas DataFrame.astype()
method. This method cast pandas's object to a specified type that means it allows us to convert the datatypes from one type to another. We can change the specified column datatype using the dictionary. The below shows the syntax of DataFrame.astype()
method.
Syntax
DataFrame.astype(dtype, copy=True, errors='raise')
Parameters
dtype: data type, or dict of column name -> data type. Use a numpy.dtype
or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy.dtype
or Python type to cast one or more of the DataFrame’s columns to column-specific types.
copy: It represents the bool(True or False), and the default is True. It returns a copy when copy=True.
errors: It includes ‘raise’, ‘ignore’, and the default is ‘raise’. If it is,
Example 1: Convert the datatypes of DataFrame using the DataFrame.astype()
Method
We can convert all column datatypes of the DataFrame to another datatype using the DataFrame.astype()
method. The below example shows the same.
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)
print("----After converting datatype of DataFrame-----")
print(df.astype('int32').dtypes)
Once we run the program we will get the following result.
----Before converting datatype of DataFrame-----
A int64
B int64
dtype: object
----After converting datatype of DataFrame-----
A int32
B int32
dtype: object
Example 2: Converting single column datatype of DataFrame using the DataFrame.astype()
Method
We can convert the single-column datatype of the DataFrame to another datatype using the DataFrame.astype()
method. The below example shows the same.
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)
print("----After converting single column datatype of DataFrame-----")
print(df.astype({'A': 'int32'}).dtypes)
Once we run the program we will get the following result.
----Before converting datatype of DataFrame-----
A int64
B int64
dtype: object
----After converting single column datatype of DataFrame-----
A int32
B int64
dtype: object
Example 3: Converting single column datatype of DataFrame using the DataFrame.astype()
Method
This example is similar to the previous example, convert the single column datatype of DataFrame and check the 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)
Once we run the program we will get the following result.
----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
Conclusion
In this tutorial, we learned the Python pandas DataFrame.astype()
method. We converted the datatype column of DataFrame to another data type and checked the DataFrame.