Select to view content in your preferred language

Help with IF statement

953
2
12-18-2013 06:53 AM
DrMar
by
Deactivated User
Hi,
I have created new "Field3" in Table which should contain Y or N based on value in "Field2" but if the Field2 IS NULL ,value in Field3 should be based on value in Field1. Something like this

[ATTACH=CONFIG]29979[/ATTACH]


Any help?
Tags (2)
0 Kudos
2 Replies
DouglasSands
Deactivated User
Try this?

import arcpy

def getResult(val):
    if val == 'TRUE':
        return 'Y'
    elif val == 'FALSE':
        return 'N'
    #Error flag
    else:
        return 'E'

def test(v1, v2):
    if v1 in ['TRUE', 'FALSE']:
        return getResult(v1)
    else:
        return getResult(v2)

def main():
    #Set inputs
    feature = ''
    field1 = ''
    field2 = ''
    field3 = ''

    fields = [feild1, field2, field3]

    with arcpy.da.UpdateCursor(feature, fields) as update:
        for row in update:
            row[2] = test(row[0], row[1])
            update.updateRow(row)

    return

if __name__ == '__main__':
    main()
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Dragan,

You could use the following in the field calculator:

Pre-logic Script Code:
def update(field1, field2):
  if field2 == 'TRUE':
    return 'Y'
  elif field2 == 'FALSE':
    return 'N'
  else:
    if field1 == 'TRUE':
      return 'Y'
    elif field1 == 'FALSE':
      return 'N'


Field3 =
update( !Field1!, !Field2!)


Be sure to check 'Python' at the top of the field calculator:

[ATTACH=CONFIG]29982[/ATTACH]
0 Kudos