Use skiprows=[1] in read_csv to skip the row 1, the conversion to float should be automatic:

df = pd.read_csv('test.csv', sep = ';', decimal = ',', skiprows=[1])

output:

print(df)
     Speed        A
0      700 -72.5560
1      800 -58.9103
2      900 -73.1678
3     1000 -78.2272

print(df.dtypes)
  Speed      int64
A          float64
dtype: object

Why your code did not work

When reading the "[rpm];[N.m]" line, the csv parsers determines that the column(s) are strings, not floats. So the decimal specifier is simply ignored and the -72,556 values remain as string, with the comma.

Answer from mozway on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › valueerror: could not convert string to float
r/learnpython on Reddit: ValueError: could not convert string to float
July 23, 2022 -

I have a dollar value from a statement -52.23 and I'm simply trying to convert it to a float so I can use it in some calculations but I keep getting this error and after some research I still don't understand why.

I have the following code going through a statement pulling out each value. The values are in a csv file and the format looks like this:

07/06/2022, 07/07/2022, AMZN, Shopping, Sale, -52.23

Loop to pull the values:

with open(file, mode='r') as f:
    csv_reader = csv.reader(f)
    for row in csv_reader:
        date = row[0]
        name = row[2]
        amt = float(row[5])
        category = 'misc'
        transaction = (date, name, category, amt)
        print(transaction)

Current printed result (when doing amt = row[5])

('07/06/22', 'AMZN', 'misc', '-52.23')

Update: Header of the csv file was the issue, once I skipped that row it worked.

🌐
Statology
statology.org › home › how to fix in pandas: could not convert string to float
How to Fix in Pandas: could not convert string to float
July 16, 2022 - #attempt to convert 'revenue' from string to float df['revenue'] = df['revenue'].astype(float) ValueError: could not convert string to float: '$400.42'
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › how-to-handle-pandas-value-error-could-not-convert-string-to-float
How To Handle Pandas Value Error : Could Not Convert String To Float - GeeksforGeeks
July 23, 2025 - When the string in the dataframe contains inappropriate characters that cause problems in converting the string to a float type, the replace() method is a good and easy way to remove those characters from the string.
🌐
GitHub
github.com › dask › dask › issues › 385
dataframe read_csv (ValueError: Exception in remote process could not convert string to float) · Issue #385 · dask/dask
July 2, 2015 - I get the following error using read_csv() ValueError: Exception in remote process could not convert string to float: '94103-2585' An easy workaround for me was to include a dtype arg (dtype={"Zip Code" : "object"}. Also, same file with pandas ...
Author   bobhaffner
🌐
Saturn Cloud
saturncloud.io › blog › how-to-handle-the-pandas-valueerror-could-not-convert-string-to-float
How to Handle the pandas ValueError could not convert string to float | Saturn Cloud Blog
October 19, 2023 - In this example, we’ve employed the apply() function along with a custom lambda function. It checks each value, extracts the numeric parts, and converts them to floats, handling various special characters. ... Handling the pandas ValueError: could not convert string to float error is a common challenge in data processing.
🌐
Medium
louwersj.medium.com › solved-valueerror-could-not-convert-string-to-float-afbc5c3828e7
Solved: ValueError: could not convert string to float: ‘’ | by Johan Louwers | Medium
September 23, 2022 - ValueError: could not convert string to float: ‘’ · Unclean datasets are the main reason that, within dat science projects, a large part of time is spend on collecting, cleaning and preparing the data before the actual use. another option available is using pandas.to_numeric which can also be used to convert to a float value as shown in the example below.
🌐
Reddit
reddit.com › r/learnpython › pandas: valueerror: could not convert string to float: ''
r/learnpython on Reddit: PANDAS: ValueError: could not convert string to float: ''
April 25, 2022 -
filepathtocsv = r"me2l.txt" #this is me2l file 

data = pd.read_csv(filepathtocsv, sep ='\t', encoding = 'UTF-8-sig', error_bad_lines= False,skiprows= 7, header = None, dtype= str, skipinitialspace= True).iloc[:,2:22] #dtype prevents auto casting
#UTF - 8 with BOM reading. Skipping first row as it contains merged column - bom = byte order mark which is utf - x
#skipping column that contains asterisk and slicing row to contaning header row

data.insert(9,'Vendor ID','') #insert at index 9 
data[['Vendor ID','Name of Vendor']] = data[11].str.split(" ",1,expand = True)

#data['PO Value'] = data[19]*data[21]
data = data.replace(',','',regex = True)

cols_kept = [2,3,4,7,10,'Vendor ID','Name of Vendor',12,15,21,19]
data = data[cols_kept]

data.columns = ["Purch.Doc.","Item","PO doc type","Req.Name","Doc/Date","Vendor ID","Name of Vendor","Short Text","MATERIAL_GROUP","Outstanding on PO","Quantity"]

data = data[data['Outstanding on PO'].str.contains("VAL")==False]

data['Outstanding on PO'] = data['Outstanding on PO'].str.replace('[a-zA-Z]','',regex = True)
data['Quantity'] = data['Quantity'].str.replace('[a-zA-Z\s]','', regex = True)

# data.replace({'Outstanding on PO':{'GBP':''}}, regex = True, inplace = True)

# data['Quantity'] = data['Quantity'].str.strip()
# data['Outstanding on PO'] = data['Outstanding on PO'].str.strip()

data = data.astype({"Outstanding on PO":"float", "Quantity":"float","Purch.Doc.":str,"Item":str})

I want to convert Quantity and Outstanding on PO to float. Trying to use the regex

[a-zA-Z\s] to remove whitespace and letters from the numbers but I get this annoying error that '' couldn't be converted to a float???

A side question, how do I select and apply str.replace to multiple columns instead of:

data['Outstanding on PO'] = data['Outstanding on PO'].str.replace('[a-zA-Z\s]','',regex = True)
data['Quantity'] = data['Quantity'].str.replace('[a-zA-Z\s]','', regex = True)

'' is not even whitespace? What is it exactly?

🌐
DataScientYst
datascientyst.com › solve-valueerror-could-not-convert-string-to-float-pandas
Solve - ValueError: could not convert string to float - Pandas
October 29, 2022 - In this short guide, I'll show you how to solve Pandas or Python errors: * valueerror: could not convert string to float: '
Find elsewhere
Top answer
1 of 2
1

The following would be useful,

print (heterozygosity_df.columns)

This looks like static typing issue within pandas. What I suspect is heterozygosity_df.['chrI'] is a column in the dataframe. What I think has happened is there's a mix of strings and floats within this column. pandas has set this as a "string" but you are wanting to perform numerical operations. Thus the solution is simply

print(heterozygosity_df.dtypes) # this should state 'chrI' is a "category" or "string"
heterozygosity_df['chrI'] = heterozygosity_df['chrI'].astype(float)
print(heterozygosity_df.dtypes)

If you have multiple changes the syntax is

heterozygosity_df = heterozygosity_df.astype({'chrI':'float', 'egColumn':'category'})

I suspect there will be other errors, e.g. the header is the first row of the data column. This is because again for pandas to automatically assign a column to a "string" means there must be a string value within the column.


From the comments. I see whats happening. The easy solution is ...

replacement = {
    "chrI": 1,
    "chrII": 2,
    "chrIII": 3,
    "chrIV": 4,
    ....
}

heterozygosity_df['chr'] = heterozygosity_df['chr'].str.replace(replacement, regex=True)

From the comments ... good it works ... this is what I would have personally done ..

replacement = {
    "chrI": 1,
    "chrII": 2,
    "chrIII": 3,
    "chrIV": 4, # continue for all chromosomes
}

heterozygosity_df = pd.read_csv("file.tsv", sep="\t", header=None).set_axis(['chr', 'pos', 'het'], axis=1, copy=False)
heterozygosity_df['chr'] = heterozygosity_df['chr'].str.replace(replacement, regex=True).astype('int')

Normally str should be in place because when 'chr' is imported its an object. However, if it works thats the only thing that counts.

2 of 2
2

Ah! I finally got it to work! I used your command but I had to change it up a little bit to work with my data. The final command I used is this:

replacement = {
    "chrI": 1,
    "chrII": 2,
    "chrIII": 3,
    "chrIV": 4,
    "chrV": 5,
    "chrVI": 6,
    "chrVII": 7,
    "chrVIII": 8,
    "chrIX": 9,
    "chrX": 10,
    "chrXI": 11,
    "chrXII": 12,
    "chrXIII": 13,
    "chrXIV": 14,
    "chrXV": 15,
    "chrXVI": 16,
    "chrmt": 17
}

heterozygosity_df['chr'] = heterozygosity_df['chr'].replace(replacement, regex=False)

And with that I was able to generate plots showing all of the chromosomes! (I have to figure out how to change the axis labels from 1,2,3,4...10 back to the chromosome but that's a future problem). Thank you so much for all your help!!

🌐
Saturn Cloud
saturncloud.io › blog › how-to-handle-valueerror-could-not-convert-string-to-float-when-using-pandas-to-read-csv
How to Handle ValueError could not convert string to float When Using Pandas to Read CSV | Saturn Cloud Blog
October 4, 2023 - In the code above, we define a custom function called convert_to_float() that tries to convert a value to a float. If the conversion fails, it returns NaN. We then pass this function as an argument to the read_csv() function using the converters parameter. This parameter is a dictionary that maps column names to conversion functions. If the first two solutions do not work, you may need to clean the data after reading the CSV file. You can do this by using Pandas' string manipulation functions to remove non-numeric characters from the column.
🌐
Python.org
discuss.python.org › python help
Could not convert string to float: '' - Python Help - Discussions on Python.org
June 6, 2024 - Hi to everybody, In pandas I want to convert a string column in a float one, but I get all the time the same error message: could not convert string to float: ‘’ The content of this column is the following: 2019-08-06T16:47:07.508 So I replaced “-”, “T”, “:” and “.” with following code: climbing[“UTC time”] = climbing[“UTC time”].replace(“-”, “”, regex=True) climbing[“UTC time”] = climbing[“UTC time”].replace(“:”, “”, regex=True) climbing[“UTC time”] = climbing[“UTC time”].replace(“.”, “”,...
🌐
GitHub
github.com › pandas-dev › pandas › issues › 31821
pd.read_csv automatically casts strings into int/float · Issue #31821 · pandas-dev/pandas
February 9, 2020 - Code Sample, a copy-pastable example if possible import pandas as pd import csv d = pd.DataFrame([ ['1', '2', 3], ['3', 4, 5]]) d.to_csv('test_d', quoting=csv.QUOTE_NONNUMERIC, index=False) dr = pd.read_csv('test_d') dr.dtypes 0 int64 1 ...
Author   rusiano
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_csv.html
pandas.read_csv — pandas documentation
Specifies which converter the C engine should use for floating-point values. The options are None or 'high' for the ordinary converter, 'legacy' for the original lower precision pandas converter, and 'round_trip' for the round-trip converter.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-valueerror-could-not-convert-string-to-float
ValueError: could not convert string to float in Python | bobbyhadz
Note that floating-point numbers must have a period as the decimal point and not a comma. The following code sample causes the error. ... Copied!my_str = '3,14' # ⛔️ ValueError: could not convert string to float: '3,14' my_float = float(my_str)
🌐
Stack Exchange
datascience.stackexchange.com › questions › 110669 › could-not-convert-string-to-float-yellow
machine learning - could not convert string to float: 'YELLOW' - Data Science Stack Exchange
ML models have an understanding of vectors only. As the data has categorical features, need to encode them and apply the ML models. That is the reason you encounter ValueError: could not convert string to float: 'YELLOW'