I am looking for a script that will calculate the cumulative sum of say population (based on a presorted table that I have created) as long as the variable of ID is the same. So it might look like
this:
Hi Clinton,
Try the following:
import arcpy from arcpy import env env.workspace = r"E:\Temp\Python\test.gdb" table = "Sample" list = [] #append IDs to list with arcpy.da.SearchCursor(table, ["ID"]) as cursor: for row in cursor: list.append(row[0]) del cursor #remove duplicates list = set(list) #update CUMPOP field for id in list: with arcpy.da.UpdateCursor(table, ["POP", "CUMPOP"], "ID = {0}".format(id)) as cursor: firstTime = True for row in cursor: if firstTime: row[1] = row[0] cursor.updateRow(row) newVal = row[0] firstTime = False else: print newVal row[1] = row[0] + newVal cursor.updateRow(row) newVal = row[0] del cursor
just for reference, I found one issue in line 33:
newVal = row[0]
Should be:
newVal = row[1]
Thank you, this works perfectly!!
Well I need the running cumulative average. After I run this loop, I need to be able to remove or select all the rows with a value greater than or less than a certain value that I choose for my analysis.
Try the Dissolve—Help | ArcGIS for Desktop tool. You could dissolve based on ID and sum on either or both of your pop fields
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.