Home > front end >  Why am I getting a ValueError: could not convert string to float error?
Why am I getting a ValueError: could not convert string to float error?

Time:04-21

I'm trying to calculate a new column for a pandas dataframe which takes a column in another dataframe, uses two variables to alter the value, giving a new value. This is the current code I have to perform the calculation:

# Calculate the Max Stress
jac_output['Max Stress /MPa'] = (jac_input['Max Load'][::-1]   (float(load_zero))) / float(area*1000)

where:

load zero = 1.5
area = 45.345

However, each time I try to perform it, I get this error:

ValueError: could not convert string to float:
'45.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.34545.3

(The 45.34534 repeats for quite a while) Why does it seem to think my area is a string? It computes the value find if I take out the / float(area*1000) part, but this isn't my desired result. Any help would be greatly appreciated!

CodePudding user response:

area is a string; multiplying a string causes it to repeat that number of times.

>>> area = "45.345"
>>> area * 5
'45.34545.34545.34545.34545.345'

What you want to do is convert it to a float and then multiply it:

>>> float(area) * 1000
45345.0
  • Related