If/Else do nothing

3222
4
Jump to solution
05-17-2018 02:01 PM
City_of_Greenville__SCGIS
New Contributor III

I'm trying to write a simple if/else statement where if the value isn't found, Arcmap keeps the current field value.  I've tried using the default python command of pass but it still overwrites the values to null.  Any advice?  Code from codeblock below.

def reclass(mystring):
      if (mystring == 'C-4'):
           return 'Central Business District'

      else:

           pass

Scott

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The better way is to pass both fields to the function:

def reclass(f1, f2):
      if (f1 == 'C-4'):
           return 'Central Business District'
      else:
           return f2

and call it using:

reclass(!field1!, !field2!)

View solution in original post

4 Replies
JoshuaBixby
MVP Esteemed Contributor

In Python, the default for a function is to return None, which will get converted to NULL in your case.  Try:

def reclass(mystring):
      if (mystring == 'C-4'):
           return 'Central Business District'
      else:
           return mystring
City_of_Greenville__SCGIS
New Contributor III

I'm actually running it against a different field though so I don't want it to just return mystring, I should've stated that. 

Here's what I want:

The field being calculated is field2

reclass(!field1!)


def
reclass(mystring):
      if (mystring == 'C-4'):
           return 'Central Business District'
      else:
           return the existing value in field2 or just do nothing to field2


Does that make sense?  Can I just define field2 in the codeblock and say if else return the value for field2?

Scott

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The better way is to pass both fields to the function:

def reclass(f1, f2):
      if (f1 == 'C-4'):
           return 'Central Business District'
      else:
           return f2

and call it using:

reclass(!field1!, !field2!)
City_of_Greenville__SCGIS
New Contributor III

Perfect, this is what I was looking for.  

Scott

0 Kudos