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.

closed as off-topic by PolyGeo Jun 19 at 20:07

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

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

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)

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