HOW TO
In ArcGIS Pro, when there are multiple data records in a field, it may be necessary to analyze the data in groups especially if the dataset is large, as shown in the image below.
This article provides the workflow to group data from multiple records from a field using ArcPy in ArcGIS Pro. In this example, the multiple records from the FacilityID field are grouped together if they share the same value in the APN field.
Warning: This workflow overwrites the attribute table of the feature layer. It is recommended to export selected records or all records in a table to create a new table. Refer to ArcGIS Pro: Export a table from a map for instructions.
import arcpy table = r'<filePath>'
apnDict = {} with arcpy.da.SearchCursor(table, ['<fieldName1>', '<fieldName2>']) as cursor: for row in cursor: apnDict.setdefault(row[0], []) apnDict[row[0]].append(str(row[1])) del cursor
arcpy.DeleteIdentical_management(table, '<fieldName1>')
with arcpy.da.UpdateCursor(table, ['<fieldName1>', '<fieldName2>']) as cursor: for row in cursor: x = apnDict[row[0]] y = ', '.join(x) row[1] = y cursor.updateRow(row) del cursor
The code block below demonstrates the example of the full working script.
import arcpy table = r'C:\Users\Documents\ArcGIS\Projects\MyProject22\MyProject22.gdb\Point1' apnDict = {} with arcpy.da.SearchCursor(table, ['APN', 'FacilityID']) as cursor: for row in cursor: apnDict.setdefault(row[0], []) apnDict[row[0]].append(str(row[1])) del cursor arcpy.DeleteIdentical_management(table, 'APN') with arcpy.da.UpdateCursor(table, ['APN', 'FacilityID']) as cursor: for row in cursor: facilities = apnDict[row[0]] facilityIDs = ', '.join(facilities) row[1] = facilityIDs cursor.updateRow(row) del cursor
The image below shows multiple records from a field are grouped according to the same values in the attribute table.
Get help from ArcGIS experts
Download the Esri Support App