Casting DataFrame to Specific Datatype

To cast a DataFrame to specific datatype, call astype() method on this DataFrame and pass the datatype as parameter to this function.

In this tutorial, we will learn the syntax of DataFrame.astype() method and how to use this method to change the datatype of pandas object (DataFrame/series) using specified operation(s).

Syntax

The syntax of pandas DataFrame astype() method is

</>
Copy
DataFrame.astype(dtype, copy=True, errors='raise')

where

ParameterValueDescription
dtypedata type, or dict of column name -> data typeUse 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.
copybool, default TrueReturn a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).
errors{‘raise’, ‘ignore’}, default ‘raise’Control raising of exceptions on invalid data for provided dtype. raise : allow exceptions to be raised ignore : suppress exceptions. On error return original object.

Return Value

DataFrame.

Examples

Cast All Columns of DataFrame to Specific Datatype

In the following program, we are casting all the columns of a DataFrame to int32 using astype() method.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})

df.astype('int32')
print(df.dtypes)

Output

col1    int32
col2    int32
dtype: object

Cast Single Column in DataFrame

In the following program, we are casting a single column of a DataFrame col1 to int32 using astype() method.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})

df = df.astype({'col1': 'int32'})
print(df.dtypes)

Output

col1    int32
col2    int64
dtype: object

Conclusion

In this Pandas Tutorial, we learned how to cast single column or multiple columns to specific datatype using pandas DataFrame.astype() method.