Select to view content in your preferred language

Combining muliple values into one cell (devided by comma, based on different attribute)

629
1
Jump to solution
07-28-2021 07:39 AM
TadeášDěd
Occasional Contributor

Hi, 

is there a simple way in ArcGis Pro of combining multiple attribute1 values (with comma as a delimiter) into one cell based on attribute2 values. Here is a simple example:

TadeDd_0-1627483055572.png

As a result, I would like to get a table with two lines

attr2 | attr1

1 | "1"

2 | "2, 5"

 

Thank you for any help. I believe there is some very simple solution, but I did not find it 😕

Best from 

T. 

 

 

0 Kudos
1 Solution

Accepted Solutions
TadeášDěd
Occasional Contributor

this could be the solution (I believe it can be way shorter)

outDict = {}
columns = ['treeNumber', 'parcel']
curObj = arcpy.da.SearchCursor("tmpIntersect", columns) # tmpIntersect is original table
for row in curObj:
if row[0] in outDict:
outDict[row[0]].append(row[1])
else:
outDict[row[0]] = [row[1]]

arcpy.CreateTable_management(gdb, outputT)
arcpy.AddField_management(gdb+"\\"+outputT, "itemNumber", "SHORT")
arcpy.AddField_management(gdb+"\\"+outputT, "parcel", "TEXT")

rows = arcpy.InsertCursor(outputT, ["itemNumber","parcel"])
for x, y in outDict.items():
row = rows.newRow()
row.setValue("itemNumber",x)
row.setValue("parcel", ", ".join(y))
rows.insertRow(row)

arcpy.Delete_management(gdb+"\\tmpIntersect")

 

View solution in original post

0 Kudos
1 Reply
TadeášDěd
Occasional Contributor

this could be the solution (I believe it can be way shorter)

outDict = {}
columns = ['treeNumber', 'parcel']
curObj = arcpy.da.SearchCursor("tmpIntersect", columns) # tmpIntersect is original table
for row in curObj:
if row[0] in outDict:
outDict[row[0]].append(row[1])
else:
outDict[row[0]] = [row[1]]

arcpy.CreateTable_management(gdb, outputT)
arcpy.AddField_management(gdb+"\\"+outputT, "itemNumber", "SHORT")
arcpy.AddField_management(gdb+"\\"+outputT, "parcel", "TEXT")

rows = arcpy.InsertCursor(outputT, ["itemNumber","parcel"])
for x, y in outDict.items():
row = rows.newRow()
row.setValue("itemNumber",x)
row.setValue("parcel", ", ".join(y))
rows.insertRow(row)

arcpy.Delete_management(gdb+"\\tmpIntersect")

 

0 Kudos