Table of Contents
In Pandas, you can convert an object to a float by using the to_numeric function with the argument ‘coerce’ and setting it to True. This will convert any non-numeric values to NaN and convert the object to a float. Alternatively, you can use the astype method and specify the desired data type to convert an object to a float.
You can use one of the following methods to convert a column in a pandas DataFrame from object to float:
Method 1: Use astype()
df['column_name'] = df['column_name'].astype(float)
Method 2: Use to_numeric()
df['column_name'] = pd.to_numeric(df['column_name'])
Both methods produce the same result.
The following examples show how to use each method with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': ['18', '22.2', '19.1', '14', '14', '11.5', '20', '28'], 'assists': [5, 7, 7, 9, 12, 9, 9, 4]}) #view DataFrame print(df) team points assists 0 A 18 5 1 B 22.2 7 2 C 19.1 7 3 D 14 9 4 E 14 12 5 F 11.5 9 6 G 20 9 7 H 28 4 #check data type of each column print(df.dtypes) team object points object assists int64 dtype: object
Method 1: Use astype() to Convert Object to Float
The following code shows how to use the astype() function to convert the points column in the DataFrame from an object to a float:
#convert points column from object to float
df['points'] = df['points'].astype(float)
#view updated DataFrame
print(df)
team points assists
0 A 18.0 5
1 B 22.2 7
2 C 19.1 7
3 D 14.0 9
4 E 14.0 12
5 F 11.5 9
6 G 20.0 9
7 H 28.0 4
#view updated data types
print(df.dtypes)
team object
points float64
assists int64
dtype: object
Notice that the points column now has a data type of float64.
Method 2: Use to_numeric() to Convert Object to Float
The following code shows how to use the to_numeric() function to convert the points column in the DataFrame from an object to a float:
#convert points column from object to float
df['points'] = pd.to_numeric(df['points'], errors='coerce')
#view updated DataFrame
print(df)
team points assists
0 A 18.0 5
1 B 22.2 7
2 C 19.1 7
3 D 14.0 9
4 E 14.0 12
5 F 11.5 9
6 G 20.0 9
7 H 28.0 4
#view updated data types
print(df.dtypes)
team object
points float64
assists int64
dtype: object
Notice that the points column now has a data type of float64.
Also note that this method produces the exact same result as the previous method.