0

Using Python, how can we populate a field in a feature class automatically in increasing order?

The user must be able to provide the start number. For example, say he wants to populate values from 51 to 75 then he must be able to provide the start number that is 51.The user must provide the number after the script has been executed,for example if we have to populate the parcel_id in a fc named parcel then hows it possible,

closed as off-topic by PolyGeo Feb 15 '18 at 22:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    But what is your question? Do you expect someone here to write your code? You'll need to demonstrate a good faith effort to write an initial try before the good folks here can give you further direction. – Vince Apr 1 '15 at 11:46
  • No acutally I used arcpy.Sort_management() to sort the field values but,my requirement is now that no value is there in the field and now we have to populate the value in the field that too in increasing order so i need only the basoc concept with few lines of code – Sunny kumar Apr 1 '15 at 12:02
  • 2
    Please use the edit button beneath your question to improve it with any clarifications. Comments may or may not be read by potential answerers. Also, be sure to take the Tour. – PolyGeo Apr 1 '15 at 12:15
4

This UpdateCursor approach begins with a start number and runs continuously until there are no more rows.

import arcpy

fc = r'C:\path\to\your\fc'

startNumber = 51

with arcpy.da.UpdateCursor(fc, "myField") as cursor:
    for row in cursor:
            row[0] = startNumber
            startNumber = startNumber + 1
            cursor.updateRow(row)

If you need to also provide an end number, you will need to add logic stating that the counter needs to end at a certain number:

import arcpy

fc = r'C:\path\to\your\fc'

startNumber = 51
endNumber   = 75

with arcpy.da.UpdateCursor(fc, "myField") as cursor:
    for row in cursor:
        if startNumber <= endNumber:
            row[0] = startNumber
            startNumber = startNumber + 1
            cursor.updateRow(row)
4

Try typing "sequential numbers" into the help file. You find a page called Calculate Field examples. Near the bottom of this are a bunch of useful field calculations; sequential being one of them. You can use that in the UI or the calculate field tool which if you desire can call via python.

Not the answer you're looking for? Browse other questions tagged or ask your own question.