Convert Object to Float in Pandas

  1. Use the astype() Method to Convert Object to Float in Pandas
  2. Use the to_numeric() Function to Convert Object to Float in Pandas

In this tutorial, we will focus on converting an object-type column to float in Pandas. An object-type column contains a string or a mix of other types, whereas float contains decimal values. We will work on the following DataFrame in this article.

import pandas as pd

df = pd.DataFrame([['10.0' ,6,7,8],
                   ['1.0' ,9,12,14],
                   ['5.0' ,8,10,6]],
                  columns = ['a','b','c','d'])

print(df)
print("---------------------------")
print(df.info())

Output:

      a  b   c   d
0  10.0  6   7   8
1   1.0  9  12  14
2   5.0  8  10   6
---------------------------
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   a       3 non-null      object
 1   b       3 non-null      int64 
 2   c       3 non-null      int64 
 3   d       3 non-null      int64 
dtypes: int64(3), object(1)
memory usage: 224.0+ bytes
None

Notice the type of column 'a', which is of the object type. We will convert this object to float using pd.to_numeric() and astype() functions in Pandas.

Note

This tutorial won’t cover convert_objects() function which is deprecated and removed.

Use the astype() Method to Convert Object to Float in Pandas

Pandas provide the astype() method to convert a column to a specific type. We pass float to the method and set the parameter errors as 'raise', which means it will raise exceptions for invalid values. Example:

import pandas as pd

df = pd.DataFrame([['10.0' ,6,7,8],
                   ['1.0' ,9,12,14],
                   ['5.0' ,8,10,6]],
                  columns = ['a','b','c','d'])

df['a'] = df['a'].astype(float, errors = 'raise')

print(df.info())

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
a    3 non-null float64
b    3 non-null int64
c    3 non-null int64
d    3 non-null int64
dtypes: float64(1), int64(3)
memory usage: 224.0 bytes

Use the to_numeric() Function to Convert Object to Float in Pandas

The Pandas to_numeric() function can be used to convert a list, a series, an array, or a tuple to a numeric datatype, which means signed, or unsigned int and float type. It also has the errors parameter to raise exceptions. An example of converting the object type to float using to_numeric() is shown below:

import pandas as pd

df = pd.DataFrame([['10.0' ,6,7,8],
                   ['1.0' ,9,12,14],
                   ['5.0' ,8,10,6]], columns = ['a','b','c','d'])

df['a'] = pd.to_numeric(df['a'],errors = 'coerce')

print(df.info())

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
a    3 non-null float64
b    3 non-null int64
c    3 non-null int64
d    3 non-null int64
dtypes: float64(1), int64(3)
memory usage: 224.0 bytes
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Pandas DataFrame

  • Convert Pandas to CSV Without Index
  • Convert Pandas DataFrame to JSON