[GIS] How calculate sequential values based on group field

arcgis-desktoparcpyfield-calculator

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?

Best 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.

Related Question