Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 floatimage text in transcribedror:

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions

Question

=+Consideration was satisfied by: Shares allotted 800 Cash 2,200

Answered: 1 week ago

Question

Analyse the various techniques of training and learning.

Answered: 1 week ago