Is there a way in ArcGIS Field Calculator to concatenate fields from a column if values match in another column?
I have attached an image for reference.
Is there a way in ArcGIS Field Calculator to concatenate fields from a column if values match in another column?
I have attached an image for reference.
If your concatenation isn't going to be huge (just a handful of values rather than hundreds, or millions as Hornbydd queried) then this is quite doable in the Field Calculator. Select the Python parser, and check the "Show Codeblock" box.
In Pre-logic Script Code:
mydict = dict()
with arcpy.da.SearchCursor('testPoint',['NameField','ValueField','JoinField']) as cursor:
for row in cursor:
mydict.setdefault(row[0],[]).append(str(row[1]))
def concat(nf):
x = None
if nf in mydict:
x = ",".join(mydict[nf])
return x
Replacing testPoint
with the name of your layer, and ['NameField','ValueField','JoinField']
with a list of your three fields
And in the expression:
concat(!NameField!)
Where NameField
is the field that holds the key values.
In the first part of the Pre-logic script code, a Search Cursor is used to find all the values in the NameField
and ValueField
fields, and creating a python dictionary to store those values. Then in the function concat()
the Field Calculator updates the JoinField
with the values from the dictionary.
Here is the code in the Field Calculator window:
And the output result in the attribute table:
Note that this does not require the key values in NameField
to be in order - you will see there is a 1
in row 13 and a couple of 2
values in rows 17 & 18.