ValueError: could not convert string to float

Loading

The error message:

ValueError: could not convert string to float: 'xyz'

occurs when you attempt to convert a string into a floating-point number, but the string contains invalid characters that are not numerical.


1. Causes and Solutions

Cause 1: Non-Numeric Characters in String

If a string contains letters or special characters, float() cannot convert it.

Incorrect Code:

num = float("xyz")  # 'xyz' is not a number

Solution: Ensure the string contains only numbers before converting.

num_str = "123.45"
if num_str.replace(".", "", 1).isdigit(): # Allows one decimal point
num = float(num_str)
print(num) # Output: 123.45
else:
print("Invalid number format")

Cause 2: String Containing Comma as Decimal Separator

Python does not recognize , as a decimal point.

Incorrect Code:

num = float("1,234.56")  # Comma causes error

Solution: Remove commas before conversion.

num = float("1,234.56".replace(",", ""))  # Correct
print(num) # Output: 1234.56

Cause 3: Empty or Whitespace String

An empty string cannot be converted to float().

Incorrect Code:

num = float("")  # Empty string

Solution: Check if the string is empty before conversion.

num_str = "   "  # Contains only spaces
if num_str.strip(): # Remove spaces and check if non-empty
num = float(num_str)
else:
print("Invalid input")

Cause 4: Parsing Data from CSV or JSON Files

Sometimes, CSV/JSON files contain missing values or wrongly formatted numbers.

Incorrect Code:

import pandas as pd

df = pd.DataFrame({"price": ["12.5", "15.8", "NA", "20.1"]})
df["price"] = df["price"].astype(float) # 'NA' causes error

Solution: Use errors='coerce' in Pandas to handle invalid values.

df["price"] = pd.to_numeric(df["price"], errors="coerce")  # 'NA' becomes NaN
print(df)

Cause 5: Hidden Unicode or Special Characters

If a string contains hidden spaces or special characters, conversion fails.

Incorrect Code:

num = float("12.34 ")  # Contains an invisible Unicode space

Solution: Strip whitespace and normalize encoding.

import unicodedata

num_str = "12.34 " # Copy-pasting from web may introduce invisible spaces
num_clean = unicodedata.normalize("NFKC", num_str.strip())
num = float(num_clean)
print(num) # Output: 12.34

Leave a Reply

Your email address will not be published. Required fields are marked *