Select to view content in your preferred language

Field Calculator Sequential Numbering related to a Field

2103
4
02-22-2011 06:13 AM
DavidRahe
Emerging Contributor
Hi there,
I need to number Features sequentially, with the Python Script I found here, everything works fine and my Table was numbered from 1 to 2436. For further processing it would be nice if I could number Features in relation to a Field in the Table. I have Categories from 1 to 7 in my table and numbering should start in each Category at 1 and numbers sequentially.

i.e. like this:

Category ---  New_Number
1 ---------------1
1 ---------------2
1 ---------------3
1 ---------------4
2 ---------------1
2 ---------------2
3 ---------------1
3 ---------------2
3 ---------------3
3 ---------------4


Thanks so far
Tags (2)
0 Kudos
4 Replies
DonovanCameron
Deactivated User
hey David,

have you considered incorporating Cursors into your script? they would help with this process.

ArcGIS Geoprocessing: Python Scripting - Advanced Techniques (.PDF, ESRI Developer Summit)
Accessing Data Using Cursors
Scripting Your ArcGIS Geoprocessing Tasks with Cursors (part 2)

And once you figure out how you would like to use the cursors (updating selected records by category perhaps?) the help on Field Calculator includes this python script example for generating sequential numbers: [ATTACH]4957[/ATTACH]
0 Kudos
DavidRahe
Emerging Contributor
Hi Don,
thanks for the fast reply,
originally I was thinking about somthing like a "where" clause or somthing like this but your tip sounds very good.

I´m not (yet) very familiar with python, so I will need approx. one or two days for the cursor implementation. This is no problem, I don´t need a solution ASAP, and I think getting used to cursors will speed up my work on longtime 😉

If I get stuck somwhere I´ll cry here for new advice ^^
Thanks alot
Dave
0 Kudos
KimOllivier
Honored Contributor
Use a search cursor (read only) to get out the objectid and your counters into a dictionary. Then you can manipulate the list using direct tools on libraries. Finally open an update cursor and put back the changes.
It is very hard to process using a cursor otherwise because you can only step through in one direction.
0 Kudos
RDHarles
Regular Contributor
You could do something like this.

Assumes:
1.) The Frequency Tool was run and the results were joined to Table.dbf.
2.) The "category numbers" are in a field called 'Category'.
3.) A field called 'Seq' exists to write the sequential numbers to.


My test table looked like this:

Category  Seq   Frequency
1 --------- 1 ----- 4
1 ---------            2           ----- 4
1 ---------           3           ----- 4
1 ---------           4 -----           4
2            --------- 1           ------2
2            --------- 2           ------2
3 ---------           1           ----- 4
3 ---------           2           ----- 4
3            --------- 3           ----- 4
3            --------- 4 -----          4

import arcpy

count=0
# Loop through the table
rows = arcpy.UpdateCursor("Table.dbf")
for row in rows:    
    # Get the values in the Category & FREQUENCY fields
    num = row.getValue("Category")
    freq = row.getValue("FREQUENCY")
    # Add one to the count
    count=count+1
    # If count is <= the frequency
    if count <= freq:        
        print "Field value "+str(num)+" assigned a "+str(count)
        # Write count in field Seq
        row.Seq = count
        # Execute the new values to the table
        rows.updateRow(row)
    # Reset counter when freq is reached.
    if count == freq:
        count=0        

print "\nDone.\n"   
0 Kudos