Question: Python I am currently getting this error: ValueError could not convert string to float ror: I believe it is coming from one text file that

Python

I am currently getting this error:

ValueError could not convert string to floatPython I am currently getting this error: ValueError could not convert stringror:

I believe it is coming from one text file that has a string in it. How would I check? The file has 50,001 rows.

Code:

import numpy as np

import pandas

import matplotlib.pyplot as plt

import glob

import sys

import re

from tabulate import tabulate

def last_9chars(x):

return(x[-9:])

files = sorted(glob.glob('Data*.txt'),key = last_9chars)

filecount = 0

n = 0

txt = []

Mean = []

Std = []

maxfilecount = int(sys.argv[1]) if len(sys.argv) > 1 else len(files)

for f in files:

filecount +=1

my_data = np.loadtxt(f, delimiter='\t')

mean = np.mean(my_data[:,1])

std = np.std(my_data[:,1])

print(f"Here {n}")

txt.append(f)

Mean.append(mean)

Std.append(std)

n +=1

if filecount >= maxfilecount:

break

d = {'File': txt,'Mean': Mean,'Standard Deviation': Std}

Temp = pandas.DataFrame(d)

Temp.to_excel("Data.xlsx",sheet_name='Sheet_name_1')

Traceback (most recent call last): File "Data.py", line 22, in my_data File "/home/paul/.local/lib/python3.6/site-packagesumpy/libpyio.py", line 1146, in loadtxt for x in read_data(_loadtxt_chunksize): File "/home/paul/.local/lib/python3.6/site-packagesumpy/libpyio.py", line 1074, in read_data items np.loadtxt(f, delimiter='\t') [Conv(val) for (conv, val) in zip(converters, vals)] File "/home/paul/.local/lib/python3.6/site-packagesumpy/libpyio.py", line 1074, in items = [conv(val) for (conv, val) in zip(converters, vals)] File "/home/paul/.local/lib/python3.6/site-packagesumpy/libpyio.py", line 781, in floatconv return float(x) valueError: could not convert string to float

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!