Convert String with Comma to Float in Python: 3 Easy Methods

Recently, I was working on a data analysis project where I needed to process a large CSV file containing financial data. The numbers were formatted with commas as thousand separators (like “1,234.56”), but Python’s float() function wouldn’t accept them directly. This is a common challenge when dealing with real-world data.

In this article, I’ll show you three useful methods to convert comma-separated strings to float values in Python. These techniques have saved me countless hours in my data processing workflows, and I’m confident they will help you too.

Let’s dive in!

Convert String with Comma to Float in Python

Now I am going to explain three important methods to convert string with comma to float in Python.

Read How to Convert String to Base64 in Python

Method 1: Use the replace() Method

The simplest way to convert a string with commas to a float in Python is by removing the commas first with the string’s replace() method.

def comma_to_float(string_value):
    # Remove commas from the string
    cleaned_string = string_value.replace(',', '')
    # Convert to float and return
    return float(cleaned_string)

# Example usage
price_string = "1,234.56"
price_float = comma_to_float(price_string)

print(f"Original string: {price_string}")
print(f"Converted float: {price_float}")
print(f"Type: {type(price_float)}")

Output:

Original string: 1,234.56
Converted float: 1234.56
Type: <class 'float'>

I executed the above example code and added the screenshot below.

Convert String with Comma to Float in Python

This method is easy and works perfectly for simple cases. I’ve used it extensively when processing sales data where prices are formatted with commas.

Check out How to Convert a String to a Float in Python?

Method 2: Use Regular Expressions

For more complex scenarios, you might need a more powerful approach using Python regular expressions. This is especially useful when dealing with various number formats.

import re

def regex_comma_to_float(string_value):
    # Remove all non-numeric characters except decimal point
    cleaned_string = re.sub(r'[^\d.]', '', string_value)
    # Convert to float and return
    return float(cleaned_string)

# Example usage
price_string = "$1,234.56"
price_float = regex_comma_to_float(price_string)

print(f"Original string: {price_string}")
print(f"Converted float: {price_float}")

Output:

Original string: $1,234.56
Converted float: 1234.56

I executed the above example code and added the screenshot below.

Python Convert String with Comma to Float

The regex approach is more flexible as it can handle additional characters like currency symbols. I find this particularly useful when dealing with financial reports or web-scraped data where numbers might come with various formatting.

Read How To Convert JSON String To Dictionary In Python

Method 3: Use locale Module

Python’s locale module provides tools for working with different regional formatting. This is an elegant solution if you’re dealing with numbers in specific locales.

import locale

def locale_comma_to_float(string_value):
    # Set US locale (for comma as thousand separator)
    locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
    # Parse the string and convert to float
    return locale.atof(string_value)

# Example usage
try:
    price_string = "1,234.56"
    price_float = locale_comma_to_float(price_string)

    print(f"Original string: {price_string}")
    print(f"Converted float: {price_float}")
except locale.Error:
    print("Locale not supported on this system. Try method 1 or 2 instead.")

Output:

Original string: 1,234.56
Converted float: 1234.56

I executed the above example code and added the screenshot below.

How to Convert String with Comma to Float in Python

The locale method is particularly useful when working with international data. I’ve used it in projects where I needed to process financial reports from different countries with varying number formats.

Check out How to Count Characters in a String in Python?

Handle Multiple Regional Formats

Sometimes you might need to handle numbers that use different formats. For instance, some countries use commas as decimal separators and dots as thousand separators (e.g., “1.234,56”).

def flexible_number_converter(string_value, decimal_char='.'):
    # Determine which character is the decimal separator
    if decimal_char == '.':
        thousand_char = ','
    else:
        thousand_char = '.'

    # Remove thousand separators and replace decimal separator with a dot
    cleaned_string = string_value.replace(thousand_char, '')
    if decimal_char != '.':
        cleaned_string = cleaned_string.replace(decimal_char, '.')

    # Convert to float and return
    return float(cleaned_string)

# Examples
us_price = "1,234.56"  # US format
eu_price = "1.234,56"  # European format

print(f"US format: {us_price} → {flexible_number_converter(us_price)}")
print(f"EU format: {eu_price} → {flexible_number_converter(eu_price, ',')}")

Output:

US format: 1,234.56 → 1234.56
EU format: 1.234,56 → 1234.56

This approach has been invaluable when working on international projects where I needed to process data from both US and European sources.

Read Split Strings with Multiple Delimiters in Python

Process Comma-Separated Numbers in Pandas

If you’re working with data analysis, you’re likely using Pandas. Here’s how to handle comma-separated numbers when reading a CSV file:

import pandas as pd

# Example CSV content with comma-separated numbers
csv_data = """Product,Price,Sales
Widget A,"1,234.56","10,000"
Widget B,"2,345.67","5,000"
Widget C,"3,456.78","2,500"
"""

# Write to a temporary file
with open('temp_data.csv', 'w') as f:
    f.write(csv_data)

# Read with pandas, specifying thousands separator
df = pd.read_csv('temp_data.csv', thousands=',')

print(df)
print("\nData types:")
print(df.dtypes)

Output:

    Product     Price  Sales
0  Widget A  1234.56  10000
1  Widget B  2345.67   5000
2  Widget C  3456.78   2500

Data types:
Product    object
Price     float64
Sales       int64
dtype: object

Pandas makes this process incredibly simple with the thousands parameter. This method has saved me countless hours when analyzing sales data from various US retailers.

Check out Convert Hexadecimal String to Integer in Python

Practical Example: Analyze Household Income Data

Let’s look at a practical example of processing income data for different US states:

import pandas as pd

# Sample data: Median household income by state with comma formatting
data = {
    'State': ['California', 'Texas', 'New York', 'Florida', 'Illinois'],
    'Median Income': ['$78,672', '$64,034', '$72,108', '$59,227', '$69,187'],
    'Sample Size': ['4,500', '3,750', '2,900', '3,200', '2,800']
}

# Create DataFrame
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# Function to convert string with comma to float
def clean_currency(x):
    if isinstance(x, str):
        return float(x.replace('$', '').replace(',', ''))
    return float(x)

# Apply the function to convert columns
df['Median Income'] = df['Median Income'].apply(clean_currency)
df['Sample Size'] = df['Sample Size'].apply(lambda x: int(x.replace(',', '')))

print("\nCleaned DataFrame:")
print(df)

# Now we can perform calculations
print("\nAverage median income:", f"${df['Median Income'].mean():.2f}")
print("Total sample size:", df['Sample Size'].sum())

Output:

Original DataFrame:
        State Median Income Sample Size
0  California       $78,672       4,500
1       Texas       $64,034       3,750
2    New York       $72,108       2,900
3     Florida       $59,227       3,200
4    Illinois       $69,187       2,800

Cleaned DataFrame:
        State  Median Income  Sample Size
0  California        78672.0         4500
1       Texas        64034.0         3750
2    New York        72108.0         2900
3     Florida        59227.0         3200
4    Illinois        69187.0         2800

Average median income: $68645.60
Total sample size: 17150

This example demonstrates how to clean and analyze real-world data with comma-separated numerical values. I’ve used similar techniques when analyzing census data and economic indicators.

I hope you found this article helpful. Converting strings with commas to float values is a common task in data processing, and having these techniques in your toolkit will save you time and frustration. Whether you’re a data scientist, financial analyst, or web developer, these methods will help you handle numeric data more effectively in your Python projects.

Other Python articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.