HOW TO

Subtract field values and exclude negative results using Python in ArcGIS Pro

Last Published: February 14, 2024

Summary

In ArcGIS Pro, field values can be subtracted from a specified value and return negative results, as shown in the image below. However, it is necessary to prevent negative results as it may lead to errors or unexpected results in certain geospatial operations. Negative distances can be more challenging to interpret in proximity analyses or buffer zone calculations. For instance, if a buffer is created around a feature and results in a negative distance, it might indicate an error in the analysis or data.

Negative values are returned when subtracting

This article provides the workflow to subtract field values and exclude negative results using Python in ArcGIS Pro.

Procedure

  1. Open the project in ArcGIS Pro.
  2. In the Contents pane, right-click the feature layer containing values for subtraction and click Attribute Table.
  3. Open the Calculate Field tool on the attribute table of the feature layer.
    1. For Input Table, select the feature layer in the drop-down list.
    2. Select the field containing values for subtraction in the Field Name (Existing or New) drop-down list. In this example, the Subtract field is selected.
    3. For Expression Type, select Python 3 in the drop-down list.
    4. Specify the Subtract field with the following Python expression in the Expression text box. Replace <fieldName> with the field name. In this example, it is Subtract.
fancy_subtract(!<fieldName>!)
  1. Specify the following Python expression in the Code Block and click OK. Replace <subtractionValue> with the desired value to be subtracted. In this example, 1200 is specified.
def fancy_subtract(subtract_from):
  value = <subtractionValue>
    if subtract_from < value:
        return 0
    else:
        diff = subtract_from - value 
        return diff
The Calculate Field tool to be configured

The image below shows non-negative values when the subtraction results in a negative number.

The values in the field return a 0 when the subtraction results in a negative number

Article ID: 000031888

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