Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to String, String to Integer, etc.
There are 2 methods to convert Integers to Floats:
Method 1: Using DataFrame.astype() method
Syntax :
DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)
Example 1: Converting one column from int to float using DataFrame.astype()
Python3
import pandas as pd
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176],
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
df = pd.DataFrame(player_list, columns=[
'Name', 'Age', 'Weight', 'Salary', 'Strike_rate'])
print(df.dtypes)
|
Output:

Let’s convert weight type to float
Python3
df['Weight'] = df['Weight'].astype(float)
print()
print(df.dtypes)
df
|
Output:

In the above example, we change the data type of column ‘Weight‘ from ‘int64’ to ‘float64’.
Example 2: Converting more than one column from int to float using DataFrame.astype()
Python3
import pandas as pd
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176],
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
df = pd.DataFrame(player_list, columns=[
'Name', 'Age', 'Weight', 'Salary', 'Strike_rate'])
print(df.dtypes)
|
Output:

Let’s convert age and strike_rate to float type
Python3
df = df.astype({"Age":'float', "Strike_rate":'float'})
print()
print(df.dtypes)
df
|
Output:

In the above example, we change the data type of columns ‘Age‘ and ‘Strike_rate’ from ‘int64’ to ‘float64’.
Method 2: Using pandas.to_numeric() method
Syntax:
pandas.to_numeric(arg, errors=’raise’, downcast=None)
Example 1: Converting a single column from int to float using pandas.to_numeric()
Python3
import pandas as pd
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176], 4
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
df = pd.DataFrame(player_list, columns=[
'Name', 'Age', 'Weight', 'Salary', 'Height'])
print(df.dtypes)
|
Output:

Let’s convert weight from int to float
Python3
df['Weight'] = pd.to_numeric(df['Weight'], downcast='float')
print()
print(df.dtypes)
df
|
Output:

In the above example, we change the data type of column ‘Weight‘ from ‘int64’ to ‘float32’.
Example 2:
Python3
import pandas as pd
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176],
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
df = pd.DataFrame(player_list, columns=[
'Name', 'Experience', 'Weight', 'Salary', 'Height'])
print(df.dtypes)
|
Output:

Let’s convert experience and height from int to float
Python3
df['Experience'] = pd.to_numeric(df['Experience'], downcast='float')
df['Height'] = pd.to_numeric(df['Height'], downcast='float')
print()
print(df.dtypes)
df
|
Output:

In the above example, we change the data type of columns ‘Experience’ and ‘Height’ from ‘int64’ to ‘float32’.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
25 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...