In this post, we will see how to convert column to float in Pandas.
[toc]
Convert String column to float in Pandas
There are two ways to convert String column to float in Pandas.
Using asType(float) method
You can use asType(float) to convert string to float in Pandas.
Here is the syntax:
df['Column'] = df['Column'].astype(float)
Here is an example. We will convert data type of Column Rating from object to float64
Sample Employee data for this Example.
[table id=30 /]
import pandas as pd
emp_df = pd.DataFrame({'Name': ['Andy','Prateek'],
'Rating': ['8.6','7']})
print("-------Original Dataframe-------\n",emp_df)
print("-------Original Dataframe's data types-------\n",emp_df.dtypes)
emp_df['Rating'] = emp_df['Rating'].astype(float)
print("-------Updated Dataframe with float data type for Rating column-------\n",emp_df)
print("-------Updated Dataframe's data types-------\n",emp_df.dtypes)
Output:
-------Original Dataframe-------
Name Rating
0 Andy 8.6
1 Prateek 7
-------Original Dataframe's data types-------
Name object
Rating object
dtype: object
-------Updated Dataframe with float data type for Rating column-------
Name Rating
0 Andy 8.6
1 Prateek 7.0
-------Updated Dataframe's data types-------
Name object
Rating float64
dtype: object
Using toNumeric() method
You can use toNumeric() method where you might have non-numeric values in the column.
Here is the syntax:
df['Column'] = pd.to_numeric(df['Column'],errors='coerce')
If column as non-numeric values, then they will be converted to NaN.
Here is sample Employee Data for this example.
[table id=31 /]
Let’s say Rating is not available for new employee Riyanka and Sneha. In this scenario, we will convert Rating column data type from String to float using toNumeric() method.
import pandas as pd
emp_df = pd.DataFrame({'Name': ['Andy','Riyanka','Prateek','Sneha'],
'Rating': ['8.6','NA','7','NA']})
print("-------Original Dataframe-------\n",emp_df)
print("-------Original Dataframe's data types-------\n",emp_df.dtypes)
emp_df['Rating'] = pd.to_numeric(emp_df['Rating'],errors='coerce')
print("-------Updated Dataframe with float data type for Rating column-------\n",emp_df)
print("-------Updated Dataframe's data types-------\n",emp_df.dtypes)
Output:
-------Original Dataframe-------
Name Rating
0 Andy 8.6
1 Riyanka NA
2 Prateek 7
3 Sneha NA
-------Original Dataframe's data types-------
Name object
Rating object
dtype: object
-------Updated Dataframe with float data type for Rating column-------
Name Rating
0 Andy 8.6
1 Riyanka NaN
2 Prateek 7.0
3 Sneha NaN
-------Updated Dataframe's data types-------
Name object
Rating float64
dtype: object
As you can see, we have converted Rating column to float64.
Convert Integer column to float in Pandas
There are two ways to convert Integer column to float in Pandas.
Using asType(float) method
You can use asType(float) to convert string to float in Pandas.
Here is the syntax:
df['Column'] = df['Column'].astype(float)
Here is an example. We will convert data type of Column Salary from integer to float64
import pandas as pd
emp_df = pd.DataFrame({'Name': ['Rohan','Abhishek'],
'Salary': [80000,40000]})
print("-------Original Dataframe-------\n",emp_df)
print("-------Original Dataframe's data types-------\n",emp_df.dtypes)
# Converting int to float data type
emp_df['Salary'] = emp_df['Salary'].astype(float)
print("-------Updated Dataframe with float data type for Salary column-------\n",emp_df)
print("-------Updated Dataframe's data types-------\n",emp_df.dtypes)
Output:
-------Original Dataframe-------
Name Salary
0 Rohan 80000
1 Abhishek 40000
-------Original Dataframe's data types-------
Name object
Salary int64
dtype: object
-------Updated Dataframe with float data type for Salary column-------
Name Salary
0 Rohan 80000.0
1 Abhishek 40000.0
-------Updated Dataframe's data types-------
Name object
Salary float64
dtype: object
Using toNumeric() method
You can use toNumeric() method where you might have non-numeric values in the column.
Here is the syntax:
df['Column'] = pd.to_numeric(df['Column'], downcast='float')
Here is an example:
import pandas as pd
emp_df = pd.DataFrame({'Name': ['Neha','Amrita'],
'Salary': [70000,60000]})
print("-------Original Dataframe-------\n",emp_df)
print("-------Original Dataframe's data types-------\n",emp_df.dtypes)
emp_df['Salary'] = pd.to_numeric(emp_df['Salary'], downcast='float')
print("-------Updated Dataframe with float data type for Rating column-------\n",emp_df)
print("-------Updated Dataframe's data types-------\n",emp_df.dtypes)
Output:
-------Original Dataframe-------
Name Salary
0 Neha 70000
1 Amrita 60000
-------Original Dataframe's data types-------
Name object
Salary int64
dtype: object
-------Updated Dataframe with float data type for Rating column-------
Name Salary
0 Neha 70000.0
1 Amrita 60000.0
-------Updated Dataframe's data types-------
Name object
Salary float32
dtype: object
That’s all about pandas convert column to float.