2

I was wondering if it's possible to use the standard auto increment code to work on groups of records:

FeatureClass = "newFC" 
recs = collections.defaultdict(int) autoIncrementEXP = "autoIncrement()" 
expCalc = """def autoIncrement(!Group!): recs[!Group!] += 1 return recs[!Group!]"""
arcpy.CalculateField_management(FeatureClass, "Number",autoIncrementEXP,"PYTHON",expCalc)

So instead of having the auto increment portion of the script stop when it reaches the end of the records, have it create sequential numbers using field value groups if that makes sense. You would start with something like the Group field below and end with the Number field containing the increment values:

Group   Number
A       1
A       2
A       3
B       1
B       2
C       1
C       2
C       3
C       4
C       5

Thanks for any insight you can provide.

3

You'll need to pass in the value of your group field to the autoIncrement function now, but this would work:

import collections

recs = collections.defaultdict(int)

def autoIncrement(field_value):
    recs[field_value] += 1
    return recs[field_value]

So something like this:

FeatureClass = "newFC"
expCalc = "import collections\n\nrecs = collections.defaultdict(int)\n\ndef autoIncrement(field_value):\n recs[field_value] += 1\n return recs[field_value]\n"
arcpy.CalculateField_management(FeatureClass, "Number", "autoIncrement(!Group!)","PYTHON", expCalc)
  • Thanks @ Jason Sceirer just to verify my code should then look like: FeatureClass = "newFC" recs = collections.defaultdict(int) autoIncrementEXP = "autoIncrement()" expCalc = """def autoIncrement(!Group!): recs[!Group!] += 1 return recs[!Group!]""" arcpy.CalculateField_management(FeatureClass, "Number",autoIncrementEXP,"PYTHON",expCalc). When I try and run this I gen an error stating 'invalid syntax line 1 unable to calculate field'. Not sure waht I'm missing. – standard Aug 25 '14 at 16:33
  • autoIncrementEXP = autoIncrement(!groupField!), expCalc = "import collections\n\nrecs = collections.defaultdict(int)\n\ndef autoIncrement(field_value):\n recs[field_value] += 1\n return recs[field_value]\n" – Jason Scheirer Aug 25 '14 at 16:36
  • It would be helpful if you would edit your question to include the code sample you included in the comment above. That will make it easier to read and test. Also, in the event that you have something that works, someone else may search for and find it, where a comment will not appear in search results. – Get Spatial Aug 25 '14 at 17:00
  • @ Get Spatial I have edited the question, apologies I got pulled aside for a minute. – standard Aug 25 '14 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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