English

Problem: The Python parser in the Field Calculator returns incorrect results

Description

In ArcMap, when the Python parser is used in the Field Calculator to carry out a division operation, integers are returned instead of decimals.

An example of the Python expression is as follows:

!VEHICLE_THEFT!/!POPULATION!

The division operation using the Field Calculator returns 0 for all fields.

[O-Image]

Cause

This is by design. Python 2.x is designed to return integers when dividing whole numbers regardless of the field type. For example, 1/9=0.

Solution or Workaround

In the attribute table, create a float type field. This allows Python to return a decimal in the result. After creating the new field, apply the Python script.

  1. Create a new field type as float in the attribute table.
    1. Open ArcMap.
    2. Right-click the selected layer in the Table Of Contents, and select Open Attribute Table.
[O-Image]
  1. Click the Table options drop-down, and select Add Field.
[O-Image] [O]
  1. In the Add Field dialog box, set the field name, and select Float as the field type, and click OK.
[O-Image]
  1. Apply the Python script.
    1. Right-click the new field, and select Field Calculator.
[O-Image]
  1. If a warning on calculating outside of an edit session displays, click Yes.
  2. In the Field Calculator, type the script highlighted below.
float(!FieldName1!/!FieldName2!)
Note:
 The code provided is for the input of the type float.
  1. Click OK.
[O-Image]

    Related Information