Select to view content in your preferred language

Populate field with max value from other fields in same table

2397
4
05-11-2011 08:17 AM
DonJohnson
Emerging Contributor
I am new to Python and v. 10.  I need to populate a type double field (NUM) based on the following criteria:

If field1 is equal to field2 then NUM is same value
If field1 is greater than field2 then NUM value is field1
If field1 is less than field2 then NUM value is field2

Thank you
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
You can do this using the UpdateCursor.  Ex:

table = "XY"

rows = arcpy.UpdateCursor(table)

for row in rows:
    if row.field1 > row.field2:
        row.Num = row.field1
    elif row.field1 < row.field2:
        row.Num = row.field2
    rows.updateRow(row)


del row, rows
0 Kudos
DonJohnson
Emerging Contributor
Thank you for the response but I failed to mention that I am attempting to do this in Field Calculator
0 Kudos
DonovanCameron
Deactivated User
Thank you for the response but I failed to mention that I am attempting to do this in Field Calculator


In v10, at the top of the Field Calculator, click the Radio Button beside Python to change the parser to that language.

In order to enable Python in earlier ArcGIS releases (9.x) you need to use the Field Calculator Tool found in the toolboxes.

The above script should then work for you.
0 Kudos
DonJohnson
Emerging Contributor
Thank you - I came up with the following:

def NUM(ONE,TWO):
  if ONE < TWO:
    VAL=TWO
  elif ONE > TWO:
    VAL=ONE
  elif ONE == TWO:
    VAL=ONE
  return VAL
0 Kudos