HOW TO

Split values within a field into separate rows using ArcPy

Last Published: August 18, 2023

Summary

A field may contain multiple values for a feature. In this example, the MATERIAL field records multiple types of material (CAS, UNK, DIP) used for each feature in the same row. These values can be separated into their respective rows using ArcPy in ArcGIS Pro and this article provides the workflow. 

The attribute table with values in the Material field.

Procedure

  1. In ArcGIS Pro, on the Analysis tab, click the Python drop-down menu and click Python window to open the Python window.
  2. In the Python window, follow the workflow below to split the values into separate rows within a field using the Python split() function.
    1. To import the necessary module, insert:
import arcpy
  1. To specify the input feature class and the input fields following the order up to the desired field, insert:
in_fc = r"<Feature_Class_Folder_Location>"
in_fields = ["<field_1>", "<field_2>", "<field_3>",  "<field_4>"]
  1. To specify the output feature class and the output fields, insert the following. Use a new feature class instead of using the same feature class because the edits are permanent.
out_fc = r"<New_Feature_Class_Folder_Location>"
out_fields = ["<field_1>", "<field_2>", "<field_3>", "<field_4>"]
  1. To read and identify the data specified, insert:
data = [list(row) for row in arcpy.da.SearchCursor(in_fc, in_fields)]
arcpy.management.TruncateTable(out_fc)
  1. To iterate through all the values within the specified field in both the input and output feature classes, insert:
cursor = arcpy.da.InsertCursor(out_fc, out_fields)

for row in data:
    if row[<number_of_splits>] is None:
        cursor.insertRow(row)
        continue
    for value in split_field.split(", "):
        value = row[<number_of_splits>]
        cursor.insertRow(row)

print("Completed")

The code block below shows the full script.

import arcpy

in_fc = r"C:\Users\Test\Desktop\Folder\Project\Test.gdb\Water_Main"
in_fields = ["OBJECTID", "FACILITYID", "INSTALLDATE", "MATERIAL"]

out_fc = r"C:\Users\Test\Desktop\Folder\Project\Test.gdb\Water_Main_SPLIT"
out_fields = ["OBJECTID", "FACILITYID", "INSTALLDATE", "MATERIAL"]

data = [list(row) for row in arcpy.da.SearchCursor(in_fc, in_fields)]
arcpy.management.TruncateTable(out_fc) cursor = arcpy.da.InsertCursor(out_fc, out_fields) for row in data:
  if row[3] is None:
        cursor.insertRow(row)
        continue
    for value in split_field.split(", "):
      value = row[3]
        cursor.insertRow(row) print("Completed")
  1. Press Enter on the keyboard to execute the code in the Python window.
  2. In the Catalog pane, right-click the new feature class and select Open Table to display the attribute table of the new feature class.

The attribute table below shows each value in the MATERIAL field is split into separate rows.

The attribute table with the MATERIAL field values split into separate rows.

Article ID: 000027735

Software:
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 7 x
  • ArcGIS Pro 2 x

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