Obviously some of your lines don't have valid float data, specifically some line have text id which can't be converted to float.

When you try it in interactive prompt you are trying only first line, so best way is to print the line where you are getting this error and you will know the wrong line e.g.

#!/usr/bin/python

import os,sys
from scipy import stats
import numpy as np

f=open('data2.txt', 'r').readlines()
N=len(f)-1
for i in range(0,N):
    w=f[i].split()
    l1=w[1:8]
    l2=w[8:15]
    try:
        list1=[float(x) for x in l1]
        list2=[float(x) for x in l2]
    except ValueError,e:
        print "error",e,"on line",i
    result=stats.ttest_ind(list1,list2)
    print result[1]
Answer from Anurag Uniyal on Stack Overflow
🌐
GitHub
github.com › numpy › numpy › issues › 22068
BUG: loadtxt raise an ValueError: could not convert string '0x0000' to float64 at row 0, column 1 · Issue #22068 · numpy/numpy
August 1, 2022 - Describe the issue: I got version 1.23.2. But loadtxt raise an ValueError:could not convert string '0x0000' to float64 at row 0, column 1 which works in 1.22.4 Reproduce the code example: import numpy as np from io import StringIO s = St...
Author   cxzchange
Discussions

Could not convert string to float
Hi! I have a “txt” file, which has several numbers like this: 1 2 3 4 I want my programme to sum all of them. I’ve done the following: file=open(“n38.txt”, “r”) result=0 for line in file: n=float(line) print(n) result=result+n print(“the sum is”, result) But I get the mistake: ... More on discuss.python.org
🌐 discuss.python.org
0
0
January 6, 2022
python - ValueError: count not convert string to float - Bioinformatics Stack Exchange
Traceback (most recent call last): ... ^^^^^^^^^^^^^^^^^^^^^^ File "pandas/_libs/algos_common_helper.pxi", line 42, in pandas._libs.algos.ensure_float64 ValueError: could not convert string to float: 'chrI' The above exception was the direct cause of the following exception: Traceback (most ... More on bioinformatics.stackexchange.com
🌐 bioinformatics.stackexchange.com
October 3, 2023
python - Error: could not convert string 'X' to float64 - Stack Overflow
I just want to convert a text to a list of lists using numpy, but the text contains the string: 'X' Error: could not convert string 'X' to float64. I have a text file that contains this: 5 3 X X ... More on stackoverflow.com
🌐 stackoverflow.com
python - Error could not convert string to float: '' - Stack Overflow
I have a column which does not want to change from object to float. The data from the xlsx file is always presented the same (as a number), but somehow only this column is seen as object. The numbe... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › cannot-convert-string-to-float-in-python
Cannot Convert String To Float in Python - GeeksforGeeks
July 23, 2025 - The "Cannot Convert String to Float" error in Python typically occurs when attempting to convert a string to a float data type, but the string content is not a valid numeric representation.
🌐
Codedamn
codedamn.com › news › programming
Fix ValueError: could not convert string to float
November 7, 2022 - According to the rules defined by the Python programming language, a string can be converted into a floating point datatype if it contains only numericals. If it contains anything other character like comma, spaces, or certain other characters then we face ValueError i.e. “could not convert...
🌐
Career Karma
careerkarma.com › blog › python › python valueerror: could not convert string to float solution
Python valueerror: could not convert string to float Solution | CK
December 1, 2023 - The float() method only allows you to convert strings that appear like floats. This means that you cannot convert a value if: ... A value contains non-special characters (i.e. “inf” is a special character, but “fd” is not) The “valueerror: could not convert string to float” error is raised if you fail to meet any one of the three above criteria.
🌐
Python.org
discuss.python.org › python help
Could not convert string to float - Python Help - Discussions on Python.org
January 6, 2022 - Hi! I have a “txt” file, which has several numbers like this: 1 2 3 4 I want my programme to sum all of them. I’ve done the following: file=open(“n38.txt”, “r”) result=0 for line in file: n=float(line) print(n) result=result+n print(“the sum is”, result) But I get the mistake: "ValueError: could not convert string to float: ‘1\n’. Where is the mistake?
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!!

🌐
Bobby Hadz
bobbyhadz.com › blog › python-valueerror-could-not-convert-string-to-float
ValueError: could not convert string to float in Python | bobbyhadz
We only had to remove the percent sign from the string to be able to convert it to a floating-point number in the example. Note that floating-point numbers must have a period as the decimal point and not a comma.
Find elsewhere
Top answer
1 of 5
3

You have empty strings in your pd.Series, which cannot be readily converted to a float data type. What you can do is check for them and remove them. An example script is:

import pandas as pd

a=pd.DataFrame([['a','b','c'],['2.42','','3.285']]).T
a.columns=['names', 'nums']

a['nums']=a['nums'][a['nums']!=''].astype(float)

Note: if you try to run a['nums']=a['nums'].astype(float) before selecting non-empty strings the same error that you've mentioned will be thrown.

2 of 5
1

First use this line to obtain the current dtypes:

col_dtypes = dict([(k, v.name) for k, v in dict(df.dtypes).items()])

Like so:

xls3 = pd.read_csv('path/to/file')
col_dtypes = dict([(k, v.name) for k, v in dict(xls3.dtypes).items()])
print(col_dtypes)

Copy the value that is printed. It should be like this:

{'Date': 'object', 'Karlar': 'float64', 'Konur': 'float64', ' Vesturland': 'object', ...}

Then, for the column which whose datatype you know isn't object, change it to the required type ('int32', 'int64', 'float32' or 'float64') Example: The datatypes might be detected as:

{'Date': 'object', 'Karlar': 'float64', 'Konur': 'float64', ' Vesturland': 'object', ...}

If we know Vesturland is supposed to be Float, then we can edit this to be:

col_dtypes = {
    'Date': 'object', 'Karlar': 'float64', 'Konur': 'float64', 
    ' Vesturland': 'float64', ...
}

Now, with this snippet you can find the non-numeric values:

def clean_non_numeric_values(series, col_type):
    illegal_value_pos = []
    for i in range(len(series)):
        try:
            if col_type == 'int64' or col_type == 'int32':
                val = int(series[i])
            elif col_type == 'float32' or col_type == 'float64':
                val = float(series[i])
        except:
            illegal_value_pos.append(i)
            # series[i] = None # We can set the illegal values to None 
            # to remove them later using xls3.dropna()
    return series, illegal_value_pos



# Now we will manually replace the dtype of the column Vesturland like so:
col_dtypes = {
    'Date': 'object', 'Karlar': 'float64', 'Konur': 'float64', 
    ' Vesturland': 'float64'
}

for col in list(xls3.columns):
    if col_dtypes[col] in ['int32', 'int64', 'float32', 'float64']:
        series, illegal_value_pos = (
            clean_non_numeric_values(series=xls3[col], col_type=col_dtypes[col])
        )
        xls3[col] = series
        print(illegal_value_pos)
        if illegal_value_pos:
            illegal_rows = xls3.iloc[illegal_value_pos]
            # This will print all the illegal values.
            print(illegal_rows[col])

Now you can use this information to remove the non-numeric values from the dataframe.

Warning: Since this uses a for loop, it is slow but it will help you to remove the values you don't want.

🌐
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 - #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.
🌐
Python.org
discuss.python.org › python help
Value error in Python: Could not convert string to float: Male - Python Help - Discussions on Python.org
January 24, 2024 - Hi All, I hope this message finds you well. I have encountered an error and hoping is someone could assist me in getting this resolved. I attempted to make a countplot with the Gender attribute however, when executing the code, I have received, ValueError: could not convert string to float: ...
🌐
ONEXT DIGITAL
onextdigital.com › home › valueerror: could not convert string to float: how to solve it in python
Valueerror: could not convert string to float: How to solve it in Python
December 8, 2022 - The float() function can only convert strings that include float-like integers. This indicates that you won’t be able to convert a value if: There are spaces in a value. A comma appears in a value.
🌐
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 - ... In general this method works ... shown below. This commonly is the case if your dataset is not clean and “avg_use” contains values that are not able to be converted to a float....
🌐
sebhastian
sebhastian.com › valueerror-could-not-convert-string-to-float
How to fix ValueError: could not convert string to float | sebhastian
April 28, 2023 - The float() function can only convert strings that inherently represent float values. If your string has special characters or alphabets, then Python will throw this error. Here are several other strings that will cause this error: my_str = "20%" ...
🌐
DataScientYst
datascientyst.com › solve-valueerror-could-not-convert-string-to-float-pandas
Solve - ValueError: could not convert string to float - Pandas
October 29, 2022 - 0 NaN 1 20.50 2 17.34 3 NaN 4 111.00 Name: amount, dtype: float64 · The ones in error will be replaced by NaN. No we can use mask to get only value which cause the error during the conversion to numerical values: df[pd.to_numeric(df['amount'], errors='coerce').isna()]['amount'] result is: 0 $10.00 3 4,2 Name: amount, dtype: object · To solve the errors: ValueError: could not convert string to float: '$10.00' ValueError: Unable to parse string "$10.00" at position 0 ·
🌐
Python Guides
pythonguides.com › could-not-convert-string-to-float-python
Could Not Convert String To Float In Python
April 18, 2024 - This Python tutorial explains what is ValueError: Could not convert string to float in Python and how to handle Could not convert string to float using different methods like try/except, etc.
🌐
Its Linux FOSS
itslinuxfoss.com › home › python › valueerror: could not convert string to float in python
ValueError: could not convert string to float in Python – Its Linux FOSS
November 12, 2022 - The “ValueError: could not convert string to float” occurs in Python due to the incorrect initialization of value to a string variable. The string to be converted must not contain any characters or symbols.