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
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:
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.
-
-1. This equal to simple autoincrement. OP specifically asks about groupsFelixIP– FelixIP2015年12月22日 04:41:27 +00:00Commented 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– ianbroad2015年12月22日 05:53:53 +00:00Commented 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– Jannik2015年12月22日 07:20:50 +00:00Commented 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– FelixIP2015年12月22日 08:52:20 +00:00Commented 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– crmackey2015年12月22日 13:47:38 +00:00Commented 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:
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.
Explore related questions
See similar questions with these tags.