Pandas DataFrame.astype()
The astype() method is generally used for casting the pandas object to a specified dtype.astype() function. It can also convert any suitable existing column to a categorical type.
It comes into use when we want to case a particular column data type to another data type. We can also use the input to Python dictionary to change more than one column type at once. In the dictionary, the key label corresponds to the column name, and the values label corresponds to the new data types that we want to be in the columns.
Syntax
Parameters
dtype: It uses numpy.dtype or the Python type for casting the entire pandas object to the same type. It can also use {col: dtype, ?} alternatively where col refers to the column label, and dtype is a numpy.dtype or Python type for casting one or more of the DataFrame’s columns to column-specific types.
copy: If copy=True, it returns a copy. Be careful when setting copy= False because changes to values may propagate to other pandas objects.
errors: For provided dtype, it controls the raising of exceptions on the invalid data.
- raise: It allows the exception that is to be raised.
- ignore: It ignores the exception. It returns the original object on error.
kwargs: It is a keyword argument that is to be passed on to the constructor.
Returns
casted: It returns the same type as a caller.
Example
Output
0 12 1 2 dtype: int64