1

I am new to ArcPy.

I have an attribute table with the following columns and fields

Attribute table

I want to fill up the column Combined as follows: If number contains 2 categories, "combined" will be A,B.

If number contains only one then this will be pasted to "combined" column.

For example in this case first 4 rows of "Combined" will be A,B Then 5555 is the same category so only A etc..

Update cursor uses only the same row so I cannot find a way to do this.

4

You can iterate twice, first SearchCursor then UpdateCursor. Store values in a Collections.defaultdict(list) then use set to remove duplicates and join to create a string of the list.

import arcpy
from collections import defaultdict

fc = r'C:\data.gdb\featureclass'
fields = ['number','category','combined']

d = defaultdict(list)
with arcpy.da.SearchCursor(fc, fields[:2]) as cursor:
    for num, cat in cursor:
        d[num].append(cat)
#d will now look like {1112:['A','A','B','B'],555:['B','B'],...}

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        row[2] = ', '.join(sorted(list(set(d[row[0]]))))
        cursor.updateRow(row)
| improve this answer | |

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