1

this is my code, im trying to calculate the standard deviation of an imported list which is shown below

 b=[]
    #time=[]

with open('nt.txt') as csvfile:
    data=csv.reader(csvfile,delimiter=('\t'))
    index=0
    for line in data:
            b.append(line[1])
            #out=line[0]
            #new=out.split(" ")
            #b.append(new[0])
        #else:break


   x=statistics.stdev(b)
print(x)

with b =['-0,002549', '-0,002040', '-0,001530'] as my output

i get ...

    raise TypeError(msg.format(type(x).__name__)) from None

TypeError: can't convert type 'str' to numerator/denominator
kevin
  • 131
  • 13
  • I suspect it is the comma being used for the decimal separator – NaN May 04 '17 at 11:19
  • how can i solve this .... – kevin May 04 '17 at 11:23
  • I don't have data to test with, but this link provides one possible solution https://mail.scipy.org/pipermail/scipy-user/2013-July/034840.html – NaN May 04 '17 at 11:29
  • i have improved the code .... i think.... now i would like to calculate the standard deviation but get the error as descbribed above – kevin May 04 '17 at 13:23

1 Answers1

0
results=np.array([[x],[b]]).astype(np.float32)

You have to set the type of the numpy array, not the list.

Arya McCarthy
  • 7,436
  • 3
  • 27
  • 48
  • with that done i get this... results=np.array([[x],[b]]).astype(dtype='float64') ValueError: could not convert string to float: '-0,002549' – kevin May 04 '17 at 10:36
  • That's because you're using commas instead of periods as decimal points. You can correct that using this suggestion: http://stackoverflow.com/questions/7106417/convert-decimal-mark – Arya McCarthy May 04 '17 at 17:05
  • Please consider upvoting this response. – Arya McCarthy May 08 '17 at 18:20