HOW TO

Group data from multiple records from a field using ArcPy in ArcGIS Pro

Last Published: October 11, 2023

Summary

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.

Multiple records in an attribute table

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.

Procedure

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.
  1. In ArcGIS Pro, open the attribute table containing the data.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  3. Run the following script.
    1. Define the path of the attribute table.
import arcpy

table = r'<filePath>'
  1. Create a dictionary of the field names. Replace '<fieldName1> and '<fieldName2>' with the desired field names from the attribute table. In this example, 'APN' and 'FacilityID' are used.
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
  1. Delete identical field values from '<fieldName1>'. In this example, 'APN' is used.
arcpy.DeleteIdentical_management(table, '<fieldName1>')
  1. Update the fields, '<fieldName1>' with '<fieldName2>' in the table. In this example, 'APN' and 'FacilityID' are used.
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.

The multiple records of data are grouped in categories

Article ID: 000031311

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options