HOW TO

Label related tables using ArcPy in ArcGIS Pro

Last Published: April 11, 2023

Summary

In ArcGIS Pro, related tables of any geodatabase table or feature class can be labeled based on fields using tools from the Fields toolset. This article provides the workflow to label related tables with tools from the Fields toolset by running an ArcPy script in ArcGIS Pro.

Procedure

  1. Start ArcGIS Pro and open the project.
  2. In the Contents pane, right-click the feature class and click Labeling Properties to open the Label Class pane.
The Contents pane in ArcGIS Pro
  1. In the Label Class pane, click the Label expression The Label expression icon button and configure the following parameters.
    1. For Language, select Python.
    2. Specify the following script in the Expression section. Change <keyField1>, <firstLabel1>, <firstLabel2>, key1, key2, L, L2, <label2>, and myDataTable to match the environment.
      • <keyField1> and key1 are the key fields in the feature class, used to relate to the related table.
      • <firstLabel> and L are the field in the feature class to be labeled.
      • <keyField2> and key2 are the key field in the related table.
      • L2 is the field in the related table to be labeled.
      • myDataTable is the folder path of the related table file, and name of the related table.
def FindLabel (<keyField1>, <firstLabel>):
    import arcpy
    key1 = <keyField1> # The key field in the feature class
    key2 = "<keyField2>"   # The key field in the related table
    L = <firstLabel> # The label field in the feature class
    L2 = "<label2>"   # The label field in the related table
    myDataTable = r"<path-to-related-table>"   # The related table file path
    cur = arcpy.da.SearchCursor(myDataTable, [key2, L2])
    for row in cur:
        if str(key1) == str(row[0]): 
            L = L + " " + str(row[1])
    return L

The following code block demonstrates the full working script.

def FindLabel ([ENAME], [CNAME]):
    import arcpy
    key1 = [ENAME] # Key field in feature class
    key2 = "District"   # Key field in related table
    L = [CNAME] # Label field in feature class
    L2 = "Population"   # Label field in related table
    myDataTable = r"C:\Users\IDT27\Documents\Artwrk\29840\HKPOP\HKPOP.gdb\HK2016_Census_csv"   # Path to related table
    cur = arcpy.da.SearchCursor(myDataTable, [key2, L2])
    for row in cur:
        if str(key1) == str(row[0]): 
            L = L + " " + str(row[1])
    return L
  1. Click Verify to run a test of the expression. If the expression is valid, click Apply.
The Label Class pane with the parameters to be filled

Article ID: 000029840

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • 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