Wednesday 23 August 2017

arcgis desktop - How calculate sequential values based on group field?


In field calculator i calculate sequential values in field [ORDER_ID] What need change in script to calculate sequential values based on group field [GROUP_ID]. I select values manually and use autoincrement () in Field Calculator.


Parser: Python Expression: autoIncrement() Pre-Logic Script Code:



rec=0
def autoIncrement():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec


This is the result. Field ORDER_ID is already calculated manually (i select GROUP_ID and use autoincrement). enter image description here


enter image description here


What changes script needed to use it in field calculator?



Answer



You can do this with an UpdateCursor easily in the Python Window:


import arcpy

vals = []
fields = ['ORDER_ID', 'GROUP_ID'] #your field names

with arcpy.da.UpdateCursor(your_lyr, fields) as rows:
for r in rows:
vals.append(r[1])
r[0] = vals.count(r[1])
rows.updateRow(r)

where the your_lyr variable is a reference to the table you're updating.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...