AnsweredAssumed Answered

How to Set up an expression in arcpy to calculate percent where one of the variables is the result of a previous calculation

Question asked by Diomedes275 on Aug 13, 2020
Latest reply on Aug 13, 2020 by danretired

Hi,

I have a feature class with two fields I am interested in using.  One field is "area_acres" and the other is "Survey_Pcnt".  I have calculated the field "area_acres".  My intent is to sum all the values in the field "area_acres" and then use that "summed_total" in the following expression to calculate the "Survey_Pcnt" field:  "!area_acres!/summed_total*100".  I have managed to sum all the values successfully, but when I try to add summed_total as a variable in the expression and run the script I get the error: TypeError: unsupported operand type(s) for /: 'str' and 'float'.  This error leads me to believe I'm trying to perform division between a string and floating point which wouldn't work.  Below is my code that I'm having trouble with.

 

summed_total = sum((r[0] for r in arcpy.da.SearchCursor(env.workspace + "/APE_intersect_dissolve_adequacy", ['area_acres'])))
print (summed_total)

percentage_expression = "(float(!area_acres!))"/float(summed_total)*"(float(100))"

arcpy.CalculateField_management(env.workspace + "/APE_intersect_dissolve_adequacy","Survey_Pcnt", percentage_expression, "PYTHON_9.3")

 

I have tried writing the percentage_expression many different ways to no avail.  At this point any help is greatly appreciated. Let me know if there is any other information I can provide.  I am using ArcGIS desktop 10.7.1 and arcpy.

thank you,

John

Outcomes