简体   繁体   中英

Why am I getting a ValueError: could not convert string to float error?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM