HOW TO

Exclude certain characters from field values for labels using Python in ArcGIS Pro

Last Published: October 25, 2024

Summary

In ArcGIS Pro, certain characters can be excluded from a field value in a label to improve map clarity, making the map easier to read and interpret. The split() function is typically used for this type of operation. However, in this case, zone codes are used, and there is no consistent naming convention, making this function unsuitable for the scenario.

This article provides the workflow to label features by excluding specific characters using a Python expression in ArcGIS Pro. The image below displays the field values used in this example.

The attribute table displaying field values in ArcGIS Pro

Procedure

  1. Open the ArcGIS Pro project, add the layer, and create a label expression. Refer to Steps 1 through 4 in ArcGIS Pro: Write a label expression for instructions. Select Python for Language.
Note:
In the Contents pane, on the List By Labeling tab, ensure the selected feature layer and feature class check boxes are checked to display the labels.
  1. Insert and configure the expression below in the Expression box:
    1. Replace <field_name> with the name of the field to be displayed as labels. In this example, the name of the field is 'ZoningCode'.
    2. Replace <value> with the value to be excluded from the field values when displayed as labels. In this example, the value used is '0123456789'.
def FindLabel([<field_name>]):
    return [<field_name>].translate({ord(i): None for i in '<value>'})
Note:
The <value> placeholder must include all characters to remove from the field values when labeling. In this example, to filter out all alphabetic characters (both uppercase and lowercase) and hyphens (-), replace  with 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'. This ensures that only numbers remain in the label.
The expression to filter out all alphabetic characters (both uppercase and lowercase) and hyphens (-) from the field values.
  1. Click the Verify button to validate the expression.
  2. Click Apply to run the script.
The Label expression configuration

The image below displays the map without numbers on the label.

Labels displaying field values after excluding numerical values on the map.

Article ID: 000033850

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

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options