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

3681
20
Jump to solution
08-13-2020 10:49 AM
JohnPapageorgiou
New Contributor III

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

Tags (2)
0 Kudos
20 Replies
by Anonymous User
Not applicable

Is that - before the summed_total in your actual script (-summed_total = ... )?  if it is, then the name 'summed_total' wouldn't exist.

0 Kudos
BibhashNath
New Contributor

it was typos... not working even after removing this..

###Codes:

summed_total = sum((r[0] for r in arcpy.da.SearchCursor("summary_tract_2", ['Sum_tract'])))
print(summed_total)

# This part is working to get my percent (I manually included sumtotal in my exp)
per_exp = "(!sum_tract!/1836082531.0221872)*100"
arcpy.AddField_management("summary_tract_2", "test1", "SHORT")
arcpy.CalculateField_management("summary_tract_2", "test1", per_exp)

# This part is NOT working to get my percent (here sumtotal selected in the func)
per_exp = "my_percent(!Sum_tract!, summed_total)"
codeblock = """
def my_percent(area, tot):
return area/tot * 100"""
# Add Field & Calculate Field
arcpy.AddField_management("summary_tract_2", "test2", "SHORT")
arcpy.CalculateField_management("summary_tract_2", "test2",
per_exp, "PYTHON3", codeblock)

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This part is NOT working

It is helpful to elaborate when saying something isn't working because there are lots of ways something can be broken and not work.  Are you getting an error message?  If so, what, and include the traceback. Are you getting unexpected results?  If so, what do you expect and what are you getting?

0 Kudos
BibhashNath
New Contributor

it says...

ExecuteError: ERROR 000539: Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'sum_total' is not defined
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am a bit confused because all of your code uses "summed_total" but the error message references "sum_total".  Do you have both "summed_total" and "sum_total" variables?  If not, why is the error message referencing a different variable name than in your code snippet?

0 Kudos
BibhashNath
New Contributor

summed_area = sum((r[0] for r in arcpy.da.SearchCursor("summary_eff_area", ['area'])))
print(summed_area)

per_exp = "my_percent(!area!, summed_area)"

codeblock = """
def my_percent(area, tot):
return area/tot * 100"""

# CalculateField
arcpy.CalculateField_management("summary_eff_area", "percent",
per_exp, "PYTHON3", codeblock)

 

ExecuteError: ERROR 000539: Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'summed_area' is not defined
 

 

0 Kudos
BibhashNath
New Contributor

got it done!! by another approach...

To calculate percentage: area to percent

total = float(sum(row[0] for row in arcpy.da.SearchCursor("summary_eff_area", 'area')))
print(total)
with arcpy.da.UpdateCursor("summary_eff_area", ('area', 'percent')) as uCursor:
for area, percent in uCursor:
uCursor.updateRow([area, (area / total)*100])

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am glad you got it working with an update cursor.  I mocked up some similar code as yours and ran it in the interactive Python window with no issues using Pro 2.7, so maybe it is either something related the version you are using or how or where you are calling the code from.

BibhashNath
New Contributor
Thanks... could you please share your code to compare line by line ...
in my case even sometimes same code do stop working..

Bibhash
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I basically just copied your code and updated the field names.  I was running it in the interactive Python window in Pro, is that where you are running the code from?

Regarding:

in my case even sometimes same code do stop working.

If your code sometimes works and sometimes doesn't, than the issue likely isn't your code and something else because valid code is valid code.

0 Kudos