Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (36.8k points)

I am a beginner in Python Datascinec and hear is the following code I am using:

#!/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]

I am getting error:

ValueError: could not convert string to float: id

When I try on only one line it works fine but when I use a loop it's throwing me an error. I am not understanding why is this happening?

>>> 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...]

As you can see it gives me the output without any error.

Any help will be appreciatable. 

1 Answer

0 votes
by (18.4k points)

If you observe correct some of the data is float and some are text that's the reason you are getting this error. You need to convert then from text to float.

When you are using only one line it works fine since it is a float value. The best way is to use an interactive prompt and print the line where you are getting error. 

#!/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]

Learn Python for Data Science Course to improve your technical knowledge. 

Browse Categories

...