Sequential Numbering Python Script for Field Calculator in Arc 10.2

1823
3
03-30-2017 05:21 AM
KennyDonaldson
New Contributor

Hi

I'm looking for a python script to populate an ID field in ACRGIS 10.2 with a sequential number that resets when the value of site field changes. e.g.

Site      ID

SIte A   0001

Site A   0002

SIte A   0003

Site A   0004

SIte A   0005

Site B   0001

SIte B   0002

Site B   0003

Site C   0001

SIte C   0002

...           .....

I've been using Arc for a number of years but have no python skills yet.

Thanks

Kenny

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Try 

old = ""
cnt = 0
def seq_count(val):
    global old
    global cnt
    if old == val:
        cnt += 1
        ret = "{} {:04.0f}".format(val, cnt)
    else:
        cnt = 0
        ret = "{} {:04.0f}".format(val, cnt)
    old = val
    return ret
__esri_field_calculator_splitter__
seq_count(!Test!)   # replace the field name with the data field

You can save this as sequential_count.cal and load it into the field calculator and it should split the code block (lines --1-13 from the expression (line 15).  Just remember to change the !Test! fieldname to match your field containing the source data.

If you prefer to copy/paste/retype, then put lines 1-13 into the code block and line 15 in the expression

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You had a question but posted it as a discussion, which likely contributed to the low response.  It is best to mark questions as questions.  Also, you could share your question with the https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=723a7387-b0...‌ place to increase the visibility and possibly get more and quicker responses.

0 Kudos
DustinHall1
New Contributor II

You can use this setup under field calculator-

Parser: Python

Check Show Codeblock

Under Pre-logic Code Script:

d={}
def GroupOrder(groupID):
N=d.get(groupID,0);N+=1
d[groupID]=N
return N

Bottom Box:

GroupOrder (!FieldName!)

Hope this helps!

0 Kudos