How to change existing field values with sequential numbers

898
6
12-21-2019 11:14 PM
eyadghatasheh
New Contributor III

hi

I have a feature class that has a field in attribute table with values like WBD001. Now I am cutting this feature into a parking lots and want to assign each parking lot a value like WBD001-1,WBD001-2, ...n. 

is there any code to do so; considering I am working on a versioned GDB and don't have the privilege to add another field to make concatenation.

Thanks

Tags (1)
0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Eyad,

Will each new record simply increment by 1 (i.e. WBD001-1,WBD001-2).  Will WBD001 ever change for other features?

0 Kudos
eyadghatasheh
New Contributor III

Hi Jake

as you said; each new record simply increment by 1 and will not ever change for other feature.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

You could use the following to do this:

def gen(x=1):
    while True:
        yield "WBD001-{0}".format(x)
        x+=1

g = gen()

Ex:

XanderBakker
Esri Esteemed Contributor

Hi eyad ghatasheh and Jake Skinner ,

I think in Jake's example he used the attribute calculation rule syntax that might not work in a normal field calculation. In case you have different original codes and want to use a sequential number within each group (original code) like shown below:

... it might be easier to create a small script and run that in the Python window:

fc = 'the name of your featureclas'
fld_name = 'the name of your field'

dct = {}
with arcpy.da.UpdateCursor(fc, (fld_name)) as curs:
    for row in curs:
        code = row[0]
        if code in dct:
            seq = dct[code] + 1
        else:
            seq = 1
        dct[code] = seq
        row[0] = "{0}-{1}".format(code, seq)
        curs.updateRow(row)

Please note that this will overwrite the original code. It would be a good idea to create a copy of the data for safety before running the script. 

eyadghatasheh
New Contributor III

Thanks a million

in fact, the test field (CAD_Design_RoomID) that I have contains "Null" for all records. and I want to generate a sequential codes for each parking Lot building on its location as each bulk of parking lots exist in an area named as WBD001, WBD002 ...etc. so each subset of parking lots should be given sequential codes like WBD001-1, WBD001-2 based on a selection as these parking lots are part of a FC that contains many other features and not only parking lots. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi abujenine ,

So if you want to work with selections and you don't have any information in the field yet (no need to create a backup), you can do this. Select the features (select by location I assume), and do the following calculation based on this support document: How To: Create sequential numbers in a field using Python in the Field Calculator 

This is the Code block:

rec=0
def autoIncrement(prefix):
    global rec
    pStart = 1
    pInterval = 1
    if (rec == 0):
        rec = pStart
    else:
        rec += pInterval
    return "{}_{}".format(prefix, rec)

and this should be your calculation:

autoIncrement("WBD001")

Change the prefix for each selection.

0 Kudos