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:

def autoIncrement():
 global rec
 pStart = 1
 pInterval = 1
 if (rec == 0): 
  rec = pStart 
  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?

2 Answers 2


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:
        r[0] = vals.count(r[1])

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

  • -1. This equal to simple autoincrement. OP specifically asks about groups
    – FelixIP
    Dec 22, 2015 at 4:41
  • 2
    @FelixIP he's incrementing by the count for each group. I don't see what's wrong with his answer.
    – ianbroad
    Dec 22, 2015 at 5:53
  • not exactly what I was asking. but it works. if I understand correctly numbering going by creation date/
    – Jannik
    Dec 22, 2015 at 7:20
  • @ian you are right. My mistake, i'll upvote it back as soon as i can. Misread count as len.
    – FelixIP
    Dec 22, 2015 at 8:52
  • @Jannik - this is doing an increment by the value in your GROUP_ID field. So it should incremental counts for each group. @ian - thanks for backing me up :)
    – crmackey
    Dec 22, 2015 at 13:47

I realise this post is a little dated and with an already accepted answer, but the question specifically asked for alterations to the existing auto-increment codeblock for the field calculator, which should be achievable simply by using the GroupID as a variable to test.

Pre-logic script would be:

def autoIncrement(GroupId):
 global rec
 global group
 pStart = 1
 pInterval = 1
 if (GroupId == group): 
  rec += pInterval 
   group += pInterval
   rec = pStart
 return rec

And the expression:


With the parser set to python. The assumptions are a) Your groups start at 1; and b) your groups are sequentially numbered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.