I am having a bit of a problem with what seems like should be a simple field calculator expression. I have a table with about 50 rows and a column with acres for each row. I've created a new column called Percent_Acres (Double) and im looking to calculate the percent each feature makes up of the total acres.
I want an expression equal to this Percent_Acres = Acres / Total Acres.
Normally this would be easy to do and i would just look up the field statistics for the sum. However this calculation is part of a model that will be automated so i need an expression that will automatically calculate the columns sum while in the field calculator.
It appears that i am having a problem with iterating through each row to add up to the total. I have tried numerous expressions and each time i get the same result telling me that "typeerror float object is not iterable"
"
Any advise on this topic would be greatly appreciated
Thanks, Ben
Solved! Go to Solution.
Hi Benjamin,
Here is a way you can do this:
def update(acres): import arcpy list = [] with arcpy.da.SearchCursor(r"C:\temp\python\test.gdb\Sample", ["Acres"]) as cursor: for row in cursor: list.append(row[0]) S = sum(list) return acres / S * 100
Hi Benjamin,
Here is a way you can do this:
def update(acres): import arcpy list = [] with arcpy.da.SearchCursor(r"C:\temp\python\test.gdb\Sample", ["Acres"]) as cursor: for row in cursor: list.append(row[0]) S = sum(list) return acres / S * 100
Thanks Jake
If I don't have "C:\temp\python\test.gdb\Sample" should I re-install Python?
That is not the value you should use, it is only a sample value in the correct format for you to imitate. You need to change the directory and feature class name to be the one you are actually calculating.
I would also change the code to make the list a global variable so that you do not run the cursor for each record (it only needs to be done once).
list = [] def update(acres): import arcpy global list if len(list) == 0: with arcpy.da.SearchCursor(r"C:\yourPath\yourGDB.gdb\yourFC", ["Acres"]) as cursor: for row in cursor: list.append(row[0]) del cursor, row S = sum(list) return acres / S * 100
Cool trick about using the global list. I didn't know you could do that in a field calc!
As the number of records being calculated grows, that trick quickly turns into a performance necessity. The same technique works for building a label expression function. See this blog on that subject. I wrote the blog the same day the light dawned on me that a global variable was essential to using a cursor inside of a loop in order to avoid doing time consuming query repetitions.
You can run a cursor against your entire feature class or related FC/table and load value lists or summaries into dictionaries/lists for the entire record set, and then build labels using the in memory data not contained in the current record, without the performance hit of building SQL statements and running the cursor on every record.
Thanks for this revised script, it's worked perfectly.
Since I'm making a tool to carry this out in ArcGISs ModelBuilder I was able to use inline variable substitution to replace r"C:\yourPath\yourGDB.gdb\yourFC" with "%Input Table%"
Right-click on the 'Calculate Field' tool within ModelBuilder, then Create Variable > From Parameter > Inpute Table. Right-click the Inpute Table and set it as a Parameter to allow user input for the tool.
If you are stuck for time, export the table to excel and do it in excel.
LOL, yep