• Register
1 vote
45 views

Problem:

I am trying to run a program like

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, it is frequently returning the same error:

ValueError: could not convert string to float: id

Do you have any idea about this error?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

Solution:

Somewhere in your text file, a line has the word id in it, which can't really be converted to a number.

Your test code works because of the word id isn't present in line 2.

If you want to catch that line, try this code. I cleaned your code up a tad:

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

for index, line in enumerate(open('data2.txt', 'r').readlines()):
    w = line.split(' ')
    l1 = w[1:8]
    l2 = w[8:15]

    try:
        list1 = map(float, l1)
        list2 = map(float, l2)
    except ValueError:
        print 'Line {i} is corrupt!'.format(i = index)'
        break

    result = stats.ttest_ind(list1, list2)
    print result[1]

 

13 9 6
94,260 points

Related questions

0 votes
1 answer 11 views
11 views
Problem: Hello guys, I am new to python programming language I basically come from c language background. I am having a problem while working with python strings. Anyway, in my program, I have got a situation where I need to convert a float into a python string. For ... the float to string I am really confused about it waiting for some help as soon as possible this is getting me in bit trouble.
asked Jul 3, 2020 Gavin 15.3k points
0 votes
1 answer 25 views
1 vote
1 answer 38 views
38 views
Problem: To check whether the input is Floating point number or not in python I wrote this program: import re # check Floating point number def checked(floatnum): # pass the regular expression # and the string in search() method if(re.search(floatnum)): print( ... line 9, in checked if(re.search(floatnum)): TypeError: search() missing 1 required positional argument: 'string' Can anyone solve this?
asked Apr 28, 2020 samhaz 5k points
0 votes
1 answer 60 views
60 views
Problem: I have a dataframe created form a JSON output that looks like this: Total Revenue Average Revenue Purchase count Rate Date Monday 1,304.40 CA$ 20.07 CA$ 2,345 1.54 % The value stored are received as string from the JSON. I am trying to: 1) Remove all characters in the ... count columns as int I tried to do the following: df[column] = (df[column].str.split()).apply(lambda x: float(x[0]))
asked Jan 22 Mashhoodch 13k points
0 votes
1 answer 119 views
119 views
Problem: Which of the following function convert a string to a float in python?Explain key differences between numbers and character strings. Use built-in functions to convert between integers, floating point numbers, and strings. Types control what operations (or methods) can be performed on a given value.
asked Feb 16 Mashhoodch 13k points
0 votes
1 answer 29 views
29 views
Problem: The error ValueError: could not convert string to float: is caused by a parsing error in the function float () with a string argument which can not be parsed as a float number. The float () function is used to convert a string with a float value to a float value. The float function parses the string and converts it as a float number.
asked Dec 2, 2020 Mashhoodch 13k points
1 vote
1 answer 4 views
4 views
Problem: I need a help , How I can fix this : python could not convert string to float
asked Mar 30 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 5 views
5 views
Problem: Could anyone please explain why I couldn't convert a string to a float numpy?
asked Apr 15 jamuna1 30.2k points
0 votes
1 answer 59 views
59 views
Problem Hey, I want to send a string to my serial. Once If I get the string or array, how can I convert my string into float value? Answer me in easy-to-understand manner as possible as you can.
asked Dec 23, 2020 chris jordan 2.4k points
0 votes
1 answer 87 views
87 views
problem: How to I Solve this: can only concatenate str (not "float") to str HELP! I need help understanding this: can only concatenate str (not "float") to str Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 7 Mashhoodch 13k points