I need to automatically calculate a field based on values from previous rows?
I need to automatically calculate a field based on values from previous rows such that once the total of all the previous rows is greater than 450 and less than 500, the next row starts
counting from 0 or the balance of what made the previous row greater than 500.
The script below just calculates the sum of the cities point that falls within the county, I want something that would make the value of a row (i.e. row2) the addition of the value of row1 + value of row2 and so on till when it gets to 500, where it starts from 1 again. Kindly help me please.
cities = "Cities"
counties = "Counties"
destField= "URBPOP"
env.workspace = "C:\Users\KUNLE\Documents\ArcGIS\Default.gdb"
env.overwriteOutput = True
fldList = arcpy.ListFields(counties)
msg = "\nFields in " + counties + ":"
arcpy.AddMessage(msg)
for aFld in fldList:
msg = " - " + aFld.name
arcpy.AddMessage(msg)
fieldPresent = False
for aFld in fldList:
if aFld.name == destField:
fieldPresent = True
if not fieldPresent:
arcpy.AddField_management(counties,destField,"DOUBLE")
msg = "\nNew Field " + destField + " created in " + counties
arcpy.AddMessage(msg)
arcpy.MakeFeatureLayer_management(cities, "cities_feat")
list = []
rows = arcpy.SearchCursor("Counties")
for row in rows:
OID = row.getValue("POP")
list.append(OID)
maxOID = list[-1]
del row, rows
x = 1
while x <= maxOID:
list2 = []
arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x))
arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat")
rows = arcpy.SearchCursor("cities_feat")
for row in rows:
pop = row.getValue("POPULATION")
list2.append(pop)
sumlist = sum(list2)
rows2 = arcpy.UpdateCursor("counties_feat")
for row2 in rows2:
row2.URBPOP = sumlist
rows2.updateRow(row2)
#msg = " - " + row2.POP + " - " + "Urban Population: " + str(sumlist)
#arcpy.AddMessage(msg)
x += 1
del row, rows, row2, rows2
arcpy.Delete_management("counties_feat")
arcpy.Delete_management("cities_feat")