Field Calculator Python If Then Statements - anthonyblackham/GIS-Wiki GitHub Wiki

When doing field calculations sometimes you'll want to modify certain fields and not others, an if then statement can help with that. Lets say we have a table where some characters are numbers and some are letters and we want to find all the numeric values and replace them with blank fields.

Lets write out what we want to happen in basic readable text:

if field is a number
return a blank field
otherwise (else)
return the original field

Now lets turn what we want to happen into code:

This is the content we put in the Pre-Logic Script Code:

def CheckNumber(field):
    if (field.isdigit()):
      return ""
    else:
      return field

Field =

CheckNumber(!Field1!)

This is how it looks in the field calculator

pythonparser

It turns this:

Field
A
5
F

into

Field
A
F

If then statements can be a very useful tool when you need more control over how your data changes when using the field calculator.