Auto-fill (field calculator) to populate field based on another sorted field

2078
2
06-03-2012 09:11 PM
SergeRafanoharana
New Contributor
Dear All,

Is it possible to auto fill a new field based on an another sorted field but not ID?

There was an article but it was based on ID and the issue was also raised there at Knowledge Base - Technical Articles

For example:

ID F1 F2
0   a  
1   d
2   c
3   a
4   b
...

Then I sort based on F1:

ID F1 F2
0   a  
3   a
4   b
2   c
1   d
...

Then I auto populate F2 (for example using field calculator)

ID F1 F2
0   a    1
3   a    2
4   b    3
2   c    4
1   d    5
...


Cheers,
Serge
0 Kudos
2 Replies
JoelCalhoun
New Contributor III
I know this isn't the Field Calculator but one way to do it is using an update cursor.

I just roughed out some python code, so it's untested but you could try this:

import arcpy

FC = "C:\\YourFeatureClass"
SORT_FIELD = "F1"
F2 = 1
# Create update cursor to populate F2   
rows = arcpy.UpdateCursor(FC,"","",SORT_FIELD, SORT_FIELD + " A") # Sort FC ASCENDING on F1 field
currentstate = ""
for row in rows:  # Loop
    if currentstate != row.SORT_FIELD:
        currentstate = row.SORT_FIELD
        row.F2 = F2
        rows.updateRow(row) # Populate F2
        F2 += 1
del row, rows # Delete to clear locks




Joel
0 Kudos
SergeRafanoharana
New Contributor
Dear Joel,

Thanks for the tips, I'll give it a try asap.

Cheers,
Sypou
0 Kudos