Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: could not convert string to float: id

I'm running the following python script:

#!/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]     list1=[float(x) for x in l1]     list2=[float(x) for x in l2]     result=stats.ttest_ind(list1,list2)     print result[1] 

However I got the errors like:

ValueError: could not convert string to float: id 

I'm confused by this. When I try this for only one line in interactive section, instead of for loop using script:

>>> from scipy import stats >>> import numpy as np >>> f=open('data2.txt','r').readlines() >>> w=f[1].split() >>> l1=w[1:8] >>> l2=w[8:15] >>> list1=[float(x) for x in l1] >>> list1 [5.3209183842, 4.6422726719, 4.3788135547, 5.9299061614, 5.9331108706, 5.0287087832, 4.57...] 

It works well.

Can anyone explain a little bit about this? Thank you.

like image 559
LookIntoEast Avatar asked Dec 07 '11 17:12

LookIntoEast


People also ask

How do you fix ValueError could not convert string to float?

The Python "ValueError: could not convert string to float" occurs when we pass a string that cannot be converted to a float (e.g. an empty string or one containing characters) to the float() class. To solve the error, remove all unnecessary characters from the string.

Could not convert string to float calc failed?

The error ValueError: could not convert string to float occurs if you try to convert a string to a float that contains invalid characters. To solve this error, check the string for characters that you can remove and use the strip() method to get rid of them.

How do you convert a string to a float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

What is float in Python?

Float() is a method that returns a floating-point number for a provided number or string. Float() returns the value based on the argument or parameter value that is being passed to it. If no value or blank parameter is passed, it will return the values 0.0 as the floating-point output.


2 Answers

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] 
like image 79
Anurag Uniyal Avatar answered Sep 28 '22 06:09

Anurag Uniyal


My error was very simple: the text file containing the data had some space (so not visible) character on the last line.

As an output of grep, I had 45  instead of just 45

like image 23
Sopalajo de Arrierez Avatar answered Sep 28 '22 05:09

Sopalajo de Arrierez