Populate a field as sequential number using autoIncrement in field calculator

4439
17
04-09-2014 07:29 AM
Highlighted
New Contributor II
Hi all,
I need to populate a field with sequencial numbers.
I could go the easy way and input in the field calculator: FIELD = 1+ [FID]

But I want my data to be first sorted so what i would like would be more something like
1) Sorting my data
2) FIELD = ROW
And so once sorted the FID is not relevant to the row number...


I've read things online and it seems that autoIncrement is my solution but I can't seems to have it working.
Here is the code I got from the help:

Expression:
autoIncrement()

Expression Type:
PYTHON_9.3

Code Block:
rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec

Here how I input it:


Here's my error message:


My field i'm trying to populate is called label and is a short integer.

Any ideas?
Tags (2)
Reply
0 Kudos
17 Replies
Highlighted
Frequent Contributor
Thomas,

This might help?

Tim
Reply
0 Kudos
Highlighted
New Contributor II
Thanks this is interresting. Sadly I've never run a python script so I don't know how to do.
Could you explain the basic idea of it?

1) sort my shapefile as fit me
2) open python console (is this the python I can find in my windows start menu?)
3) then copy the following in the console
rows = arcpy.UpdateCursor("your attriubute table")

counter = 1
for row in rows:
row.NEW_ID = counter
rows.updateRow(row)
counter += 1

What should I insted of "your attriubute table"="C:\GIS\Shapefiles\interp\interp_polygon.dbf" ???
Reply
0 Kudos
Highlighted
Frequent Contributor
Thomas,

This should help you find where you need to input this code and here is an example of an update cursor (You can see they first define FC as the path to their table)

Tim
Reply
0 Kudos
Highlighted
MVP Honored Contributor
Your original post has the correct code.  However, you did not check the Parser radio button at the top of the Field Calculator dialog to perform a Python calculation.  The code won't work as a VB Script calculation, which is what you were doing.  So go back and input that code and change the Parser radio button setting to "Python" and it will uniquely number your records.
Reply
0 Kudos
Highlighted
New Contributor II
Hi, thank you so much for your help already.
I'm not there yet but hopefully it will work soon.
Let's talk python script within field calculator first:

So to summarize:
1)I have a field called LABEL (short integer) within a attribute table of a polygon shapefile
2)I start editing and open the attribute table
3) In field calculator I input that code

rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec
Which I have tried with or without the indentations

4) I input it the same way as above but ticking the python parser tick box (screengrab was wrong)
5) It doesnt work and got these results:



=> I really cannot see what is wrong in there
=> Can it be my arcgis 10.0 (build 2414) which is doing some problem?
=> Due to IT support I'm not admin on my machine and can't do much changes is this could affect me?


I'll have a read throught the other solution you have offer see if I'm more lucky.
Thanks again for taking time to answer.

Thomas
Reply
0 Kudos
Highlighted
New Contributor II
Don't know if this is any help but here it is too.



Thanks
Reply
0 Kudos
Highlighted
New Contributor II
I have also tried the python windows without much success:
Here are a few exemples:




Hope the screen grab can help, sorry they are quiet large displayed on the screen.
Is my python just not working?
Reply
0 Kudos
Highlighted
MVP Honored Contributor
Here is the Field calculator set up specifying each item in the Field Calculator set up dialog in order.  Always include the indentations when using Python code exactly as written, indentations are essential to Python.  Capitalization can also be important in Python.  This code works in ArcGIS 9.3 and up.  No special set up or rights is required.  Paste the code exactly as written below within the code tags.  I just ran the code as shown and it worked without error.

Parser:  Python

Show Codeblock: checked

Pre-Logic Codeblock:
rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
  rec = pStart
else:
  rec = rec + pInterval
return rec


Expression (Label): autoIncrement()

The screen shot shows how the Field Calculator set up should look (I have scrolled down in the Codeblock window to show the indentation required in the lower half of the code, which your screenshot did not show.)

The error being reported is a parsing syntax error, indicating that you did not indent the code correctly according to Python requirements.  But since you have not pasted your code within code tags to preserve your indentation I cannot tell where the error is located.
Reply
0 Kudos
Highlighted
MVP Honored Contributor
Here is the required indent for the Python window code.  Indentations must be included as shown below for Python to work.  You also had the wrong field name (NEW_ID) for the 6th line.  It should read row.Label = counter.

import arcpy
rows = arcpy.UpdateCursor("C:/gis/yourgeodatabase.gdb/mag_interp_polygon")

counter = 1
for row in rows:
    row.Label = counter
    rows.updateRow(row)
    counter += 1


Most likely you have input the incorrect path and feature class name into the cursor input of the second line.  Your feature class is named "mag_interp_polygon" according to the field calculator error, not Label (the field name, which is not included in the cursor input of line 2).  I don't know the actual path or geodatabase name you are really using, but they are almost certainly wrong in what you have written and they are wrong above, since I don't know these essential pieces of information.  The path, gdb name (if applicable) and file name or the layer name must be correct for the code not to error with a 99999 error.  Screen shot the Layer Properties of the feature class you are updating on the Source Tab page to tell us the actual correct path and feature class name you need to input to the cursor.  You may also be able to input just the layer name shown in your map instead of its on disk path and file name.
Reply
0 Kudos