# Field Calculator Python IF statement

44848
6
05-28-2017 03:18 PM
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

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....)?

Marco

6 Replies
MVP Legendary Contributor

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

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

MVP Legendary Contributor

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.

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.

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.

by
Frequent 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.