Field Calculator Python IF statement

45680
6
05-28-2017 03:18 PM
MarcoOrlandi1
New Contributor II

Hi everyone,

I’m trying to use IF statements in field calculator to fill a field starting from another field’s values:

Field to fill up: Historic_period

Field already filled: Century

Statement in human language:

IF Century = 6 then write “late antiquity”, IF Century=10 the write “Middle age” etc etc.

I’m struggling trying to set up the correct syntax in ArcGis Pro Filed Calculator but I’m always missing something…..

Can someone help and guide me step by step (I don't know how to use the helpers and the Code Block....)?

Thanks in advance,

Marco

6 Replies
DanPatterson_Retired
MVP Emeritus

without getting into specifics for your case... examine this image and return with questions.  In ArcMap

And so you can see the similarities and difference in PRO

KatR_
by
New Contributor II

Thank you for the comparison, very helpful!

Do you know how to format for a string input as the condition to hunt for when calculating the field?

Ex. (Build_Code is the field to calculate, Build_Type is the conditional field)

Build_Code =
Type(!Build_Type!)

def Type(fld):
      if fld == "Neighborhood"
            value = 1
      elif fld == "Apartment"
            value = 2
      elif fld == "Condo"
            value = 3

      return value

0 Kudos
DanPatterson_Retired
MVP Emeritus

Kat, you are missing : 's at the end of your if lines

def Type(fld):
    if fld == "Neighborhood":
        value = 1
    elif fld == "Apartment":
        value = 2
    elif fld == "Condo":
        value = 3
    return value

The field you are calculating values for should be of type 'short' or 'long'

and the 'expression' for that field would be

Type(!Build_Type!)

where the !Build_Type! is the field containing the text classes.

GJB
by
New Contributor III

Hello, this is an excellent post. In PRO I am trying to do essentially the same thing as KatR, except need to replace !prim_lith! (a text field with geologic material descriptions, eg, clay) with a new !prim_lith_reclass! (a short integer field) whose value depends on the geologic material.  The Expression is valid but the return values are all equal to 1 (should be 1-9). I've tried varied syntax etc. but nothing seems to work.  Any insights would be greatly appreciated!  Thank you. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If statements are executed in order, so looking at your first statement:

if fld != "Soil or Fill:

Every record but those with "Soil or Fill" will return True, which means they all will get mapped to the value 1.  Records with "Soil or Fill" will be caught by the second statement

elif fld != "Alluvium":

and get mapped to the value 2.

In short, given your existing if statement structure, records can only have a value of 1 or 2 with most of them all being 1.

0 Kudos
Bud
by
Notable Contributor

In my opinion, calculated columns should be easier in ArcGIS Pro: Virtual Attributes (ad hoc).

I want to be able use SQL like this:  case when a<>b then 1 end  .

To me, that would be better than creating a new field in the FC and fumbling with the field calculator.

This post has ~42,000 hits. And there are ten other posts like it. That suggests to me that field calculator IF statements need improving.