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,
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
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)
if fld == "Neighborhood"
value = 1
elif fld == "Apartment"
value = 2
elif fld == "Condo"
value = 3
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
where the !Build_Type! is the field containing the text classes.
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.
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.
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.