How to convert columns datatype in python?

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

DataFrame.astype(dtype, copy=True, errors=‘raise’, **kwargs)

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

import pandas as pd  
a = {'col1': [1, 2], 'col2': [3, 4]}  
info = pd.DataFrame(data=a)  
info.dtypes  
# We convert it into 'int64' type.  
info.astype('int64').dtypes  
info.astype({'col1': 'int64'}).dtypes  
x = pd.Series([1, 2], dtype='int64')  
x.astype('category')  
cat_dtype = pd.api.types.CategoricalDtype(  
categories=[2, 1], ordered=True)  
x.astype(cat_dtype)  
x1 = pd.Series([1,2])  
x2 = x1.astype('int64', copy=False)  
x2[0] = 10  
x1  # note that x1[0] has changed too  

Output

0    12
1     2
dtype: int64