how to sequentially number a field in Field Calculator (NOT BASED ON FID)

6588
8
04-10-2015 12:46 PM
PiyaliKundu
New Contributor III

This may be a very simple question, but I have not found the answer after searching for forever, and all the code snippets I have used have been based off of the FID, which I DO NOT WANT.

I have a long list of features which I would like to ID sequentially, but not based off of the FID at all! I just want them to go down the field like (1,2,3,4, etc.). I have tried using the following snippet for Python which was supplied by ESRI, but it doesn't work as it essentially just adds 1 to the FID:

Pre-Logic Code:

rec=0

def autoIncrement():

global rec

pStart = 1 

pInterval = 1

if (rec == 0): 

  rec = pStart 

else: 

  rec += pInterval 

return rec

Code Block:

autoIncrement()

Source: 38517 - Create sequential numbers in a field using Python in the Field Calculator

0 Kudos
8 Replies
NeilGarlock
New Contributor II

A work around;

export your data as a .dbf or shape

save as an excel table

sort as desired

number as wanted

Join back to your data

Calculate the values from the excel table

not pretty but it works

DanPatterson_Retired
MVP Emeritus

You haven't indicated how you want it incremented but why not change change

pStart=0

to

pStart=10

to number for 10 for example...pStart and pInterval values can be edited in the script

Now if you want something fancier, then you will have to provide start and stop values otherwise the above code will just carry on forever since, it is the mere fact that you are running this in the field calculator that things work.

RichardFairhurst
MVP Honored Contributor

What you want is not possible to do.  The order you see in an unsorted table view is essentially random and undetectable by any means as far as I know and I have never understood why this order occurs in a table view.  Anything other than ObjectID order or a sort field order is influenced by the order of edits and changes every time you edit the table (attributes or geometry).  The mere act of calculating or altering the field value in the visible order you see would change the order of what you see in the table view after the calculation/script completed and the numbers would appear random again.

You have to have some field that you want this sorted by to get a specific order of incremented values.  You cannot use the field calculator to sort by anything other than the ObjectID, so you have to use a Python cursor, not the field calculator to get an alternative order.  See this post for how to increment based on the sort order of a given field value (if it is not unique for every record you must use the ObjectID or another field to break the tie and make it unique).

0 Kudos
DanPatterson_Retired
MVP Emeritus

Point taken...but she didn't mention sorted and the autoincrement code doesn't actually use the FID field, it just starts from index 0 and accesses each record as it encounters it, which is in sequential order

0 Kudos
RichardFairhurst
MVP Honored Contributor

Dan:

I get this by looking at her picture.  She wants whatever order the table view randomly picks without any sorting of any field to be sequentially numbered to fit that (dis)order.  She has not mentioned any field that she would like to order by and as far as I could see her picture was not sorted based on any field value at all.

The autoincrement code always uses ObjectID order, because that is the order that the field calculator and any unsorted cursor always uses to process records in a table.

DanPatterson_Retired
MVP Emeritus

I don't see any picture...just a link to the old autoincrement code???

0 Kudos
RichardFairhurst
MVP Honored Contributor

She attached a png to her post called notworking.png.  It shows a table that appears to following no particular order.

If the table shown in that picture is following a sorted order it must be based on a multifield sort.  My code can increment to follow a multifield sort by just replacing the OID@ field name with the alternative secondary sort field to control the multifield sort.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Richard...Now I see it...might be nice to say "see attachment".  In any event yes, it is sorted somehow which is why any variant of autoincrement won't work and suggested options need to be used

0 Kudos