Change Data Type for one or more columns in Pandas Dataframe


Many times we may need to convert the data types of one or more columns in a pandas data frame to accommodate certain needs of calculations. There are some in-built functions or methods available in pandas which can achieve this.

Using astype()

The astype() method we can impose a new data type to an existing column or all columns of a pandas data frame. In the below example we convert all the existing columns to string data type.

Example

 Live Demo

import pandas as pd
#Sample dataframe
df = pd.DataFrame({
   'DayNo': [1, 2, 3, 4, 5,6,7],
   'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'],
   'Qty': [2.6, 5, 11.8, 2, 5.6,0,0.25]})
# Exisitng Data types
print(df.dtypes)
#Convert to string data type
df_str = df.astype(str)
# Verify the conversion
print("***After Conversion***")
print(df_str.dtypes)

Output

Running the above code gives us the following result −

DayNo       int64
Name       object
Qty     float64
dtype: object
***After Conversion***
DayNo    object
Name     object
Qty     object
dtype: object

Using to_numeric()

We can convert the numbers which are currently marked as string in the data frame to numeric using to_numeric().

Example

 Live Demo

import pandas as pd
# Example dataframe
df = pd.DataFrame({
   'DayNo': [1, 2, 3, 4, 5,6,7],
   'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'],
   'Qty': [2.6, 5, 11.8, 2, 5.6,0,0.25]})
df_str = df.astype(str)
print(df_str.dtypes)
#Applying conversion
print("After Conversion:")
df_num = pd.to_numeric(df_str.DayNo)
print('DayNo:',df_num.dtypes)

Running the above code gives us the following result −

Output

DayNo object
Name object
Qty object
dtype: object
After Conversion:
DayNo: int64

Using infer_objects()

It is a method of soft conversion where we convert columns of a DataFrame that have an object datatype to a more specific type.

Example

import pandas as pd
# Example dataframe
df = pd.DataFrame({
   'DayNo': [1, 2, 3, 4, 5,6,7],
# 'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'],
   'Qty': ['2.6', '5', '11.8', '2', '5.6','0','0.25']}, dtype='object')
print(df.dtypes)
#Applying conversion
print("After Conversion:")
df_new = df.infer_objects()
print(df_new.dtypes)

Running the above code gives us the following result −

Output

DayNo    object
Qty      object
dtype:   object
After Conversion:
DayNo   int64
Qty    object
dtype: object

Updated on: 23-Aug-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements