Calculate and Restart Sequential ID for Unique Values in a Field

المشرف العام

Administrator
طاقم الإدارة
I need to calculate a sequential ID for unique values in a field. So, for the values in Field1, the sequential ID count should count all occurrences of that value, then restart the count for the next value. For the example below, column 1 contains the values to count, column 2 is what the sequential field result should look like.

A 1

A 2

B 1

B 2

B 3

B 4

C 1

D 1

D 2

D 3

My plan was to approach this with an update cursor and the classic autoIncrement code, but it calculated all rows in the table with a sequential ID. I know the if/else is the wrong approach, but I was hoping the autoIncrement would start over within each if statement. I'm guessing I need to get a count of the unique values in Field1, then somehow calculate the sequential ID on the row as it counts? I saw a collections.Counter example, but wasn't sure if that could be applied here.

table = table pathfields = ('FIELD1,FIELD2')rec=0def autoIncrement():global recpStart = #last unique IDpInterval = 1 if (rec == 0): rec = pStart else: rec = rec + pInterval return recwith arcpy.da.UpdateCursor(table, fields) as uc: for row in uc: if row[0] == 'A': row[1] = str(autoIncrement()) etc.Thanks!



أكثر...
 
أعلى