In this article we will discuss how to change the data type of a single column or multiple columns of a Dataframe in Python.

Change Data Type of a Single Column

To change the data type of a single column in dataframe, we are going to use a function series.astype(). Let’s first discuss about this function,

series.astype()

In Python’s Pandas module Series class provides a member function to the change type of a Series object i.e.

Arguments:

  • dtype : A python type to which type of whole series object will be converted to.
  • errors : Way to handle error. It can be : {ignore, raise}, default value is raise
    • raise: In case of invalid parsing raise an exception
    • ignore: In case of invalid parsing return the input as it original
  • copy : bool. Default value is True.
    • If False : Make changes in current object
    • If True : Return a copy

Returns:

  • If copy argument is True then returns a new Series object with updated type.

Now let’s see how to use this function to change the data type of a column in our dataframe.

Import pandas module as pd i.e.

First of all we will create a Dataframe with different data type of columns  i.e.

Contents of the dataframe are,


Now let’s check the datatype of columns in the above created dataframe,


Output:

Change data type of a column from int64 to float64

As we can see that data type of column ‘Marks’ is int64. Let’s change the data type of column ‘Marks’ to float64 i.e.

By default astype() returns a copy of passed series with changed data type. We assigned this new series back to empDfObj[‘Marks’].

Now check the data type of dataframe’s columns again i.e.

Output:


Now data type of column ‘Marks’ is float64. It will also be reflected in the contents of dataframe i.e.


Output:


In ‘Marks’ column values are in float now.

Let’s see an another example,

Change data type of a column from int64 to string

Data type of column ‘Age’ is int64. Let’s change the data type of column ‘Age’ to string i.e. object type

As default value of copy argument in astype() was True. Therefore, it returns a copy of passed series with changed data type. We assigned this new series back to empDfObj[‘Age’].

Now check the data type of dataframe’s columns again i.e.

Output:


Now data type of column ‘Age’ is object.

This is how we can change the data type of a single column in dataframe. Now let’s see how to change types of multiple columns in a single line.

Change Data Type of Multiple Columns in Dataframe

To change the data type of multiple columns in the dataframe we are going to use DataFrame.astype().

DataFrame.astype()

It can either cast the whole dataframe to a new data type or selected columns to given data types.

Arguments:

  • dtype : A python type to which type of whole dataframe will be converted to.
      • Dictionary of column names and data types. On given columns will be converted to corresponding types.
  • errors : Way to handle error. It can be : {ignore, raise}, default value is raise
      • raise: In case of invalid parsing raise an exception
      • ignore: In case of invalid parsing return the input as it original
  • copy : bool. Default value is True.
      • If False : Make changes in current object
      • If True : Return a copy

Returns

  • If copy argument is True then returns a new Dataframe object with updated type of given columns.
Let’s understand this by some examples,
First of all we will create a Dataframe i.e.

Contents of the dataframe are,


Now let’s check the datatype of columns in the above created dataframe,


Output:


Now to convert the data type of 2 columns i.e. ‘Age’ & ‘Marks’ from int64 to float64 & string respectively, we can pass a dictionary to the Dataframe.astype(). This dictionary contains the column names as keys and thier new data types as values i.e.

As default value of copy argument in Dataframe.astype() was True. Therefore, it returns a copy of passed Dataframe with changed data types of given columns. We assigned this new series back to empDfObj.

Now check the data type of dataframe’s columns again i.e.


Output:


Now the new data types of column ‘Age’ is float64 and ‘Marks’ is string.

It will be reflected in the contents of the dataframe too i.e.

Output:

Handle errors while converting Data Types of Columns

Using Series.astype() or Dataframe.astype() If we pass the type to which content can not be typecasted then it will create error. By default in case of error it will through TypeError.
For example, lets try to convert the type of a column ‘Age’ to ‘abc’. It will raise the error i.e.

Output:


As there is no data type ‘abc’, therefore if we try to convert the data type of a column to something that is not possible then it will though error TypeError and program will crash. To handle this kind of fatal error use try / except.

Complete example is as follows,

Output:

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.