0

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?

asked Dec 22, 2015 at 3:31

2 Answers 2

8

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.

FelixIP
23.5k3 gold badges32 silver badges63 bronze badges
answered Dec 22, 2015 at 4:01
5
  • -1. This equal to simple autoincrement. OP specifically asks about groups Commented 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. Commented Dec 22, 2015 at 5:53
  • not exactly what I was asking. but it works. if I understand correctly numbering going by creation date/ Commented 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. Commented 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 :) Commented Dec 22, 2015 at 13:47
1

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:

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

And the expression:

autoIncrement(!GroupId!)

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

answered Aug 15, 2018 at 2:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.