Home » How to Fix in Pandas: could not convert string to float

How to Fix in Pandas: could not convert string to float

by Tutor Aspire

One common error you may encounter when using pandas is:

ValueError: could not convert string to float: '$400.42'

This error usually occurs when you attempt to convert a string to a float in pandas, yet the string contains one or more of the following:

  • Spaces
  • Commas
  • Special characters

When this occurs, you must first remove these characters from the string before converting it to a float.

The following example shows how to resolve this error in practice.

How to Reproduce the Error

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'store': ['A', 'B', 'C', 'D'],
                   'revenue': ['$400.42', '$100.18', '$243.75', '$194.22']})

#view DataFrame
print(df)

  store  revenue
0     A  $400.42
1     B  $100.18
2     C  $243.75
3     D  $194.22

#view data type of each column
print(df.dtypes)

store      object
revenue    object
dtype: object

Now suppose we attempt to convert the revenue column from a string to a float:

#attempt to convert 'revenue' from string to float
df['revenue'] = df['revenue'].astype(float)

ValueError: could not convert string to float: '$400.42' 

We receive an error since the revenue column contains a dollar sign in the strings.

How to Fix the Error

The way to resolve this error is to use the replace() function to replace the dollar signs in the revenue column with nothing before performing the conversion:

#convert revenue column to float
df['revenue'] = df['revenue'].apply(lambda x: float(x.split()[0].replace('$', '')))

#view updated DataFrame
print(df)

  store  revenue
0     A   400.42
1     B   100.18
2     C   243.75
3     D   194.22

#view data type of each column
print(df.dtypes)

store       object
revenue    float64
dtype: object

Notice that we’re able to convert the revenue column from a string to a float and we don’t receive any error since we removed the dollar signs before performing the conversion.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix in Python: ‘numpy.ndarray’ object is not callable
How to Fix: TypeError: ‘numpy.float64’ object is not callable
How to Fix: Typeerror: expected string or bytes-like object

You may also like