Hello,
I am trying to create a python IF/THEN statement within a model that runs the field calculator tool. Essentially I want it to check to make sure if a cell has data entered (IS NOT NULL), and if so, calculate a concatenation of 4 other fields.
Sample of what I want:
IF field1 <> NULL
THEN field2 = concatenation expression
ELSE field1 still = NULL
My python skills are very rudimentary, and I have only tested off of what I've found through googling python IF/THEN statements. I don't fully understand how to take their samples and make them work with what I need to accomplish. One of the samples that I've tried to mimic is pasted below:
Expression:
Reclass(!WELL_YIELD!)
Expression Type:
PYTHON_9.3
Code Block:
def Reclass(WellYield):
if (WellYield >= 0 and WellYield <= 10):
return 1
elif (WellYield > 10 and WellYield <= 20):
return 2
elif (WellYield > 20 and WellYield <= 30):
return 3
elif (WellYield > 30):
return 4
Solved! Go to Solution.
In Python Null is None. Use != instead of <>. All if clauses and else clauses must end in a colon.
You don't set field values in a calculation you calculate a given field and return values to it. If and else cannot be capitalized (capitalization and spacing matter in Python). You must include all fields used for concatentation in the parameter list passed to the def of the method. So your sample calculation should look like this (it will assign the concatenated value or Null (None) to field2):
Expression:
Reclass(!field1!, !Otherfield!)
Expression Type:
PYTHON_9.3
Code Block:
def Reclass(field1, otherfield):
if field1 != None:
return field1 + otherfield
else:
return None
In Python Null is None. Use != instead of <>. All if clauses and else clauses must end in a colon.
You don't set field values in a calculation you calculate a given field and return values to it. If and else cannot be capitalized (capitalization and spacing matter in Python). You must include all fields used for concatentation in the parameter list passed to the def of the method. So your sample calculation should look like this (it will assign the concatenated value or Null (None) to field2):
Expression:
Reclass(!field1!, !Otherfield!)
Expression Type:
PYTHON_9.3
Code Block:
def Reclass(field1, otherfield):
if field1 != None:
return field1 + otherfield
else:
return None
I haven't had the opportunity to test this out just yet, but I do have a couple other quick questions.
1) It shouldn't matter if field1 isn't involved in the concatenation, right? Just so long as I add all of the fields within the parentheses in both sections?
2) I also don't think it would be an issue adding characters in the return statement, right? (e.g. field1 + "-" + field2 + "-" + field3 etc...
Thanks for the information. I'll post back whenever I have the opportunity to try this out.
The answer to question 1 is that any field that has to be compared using the if/then logic or that has to be used to build your concatenated return value must be in the parentheses in both section. So if you want to concatenate field1, field2 and field3 in the calculation, all 3 must be in the method parentheses. The actual field from the field list must be used in the list in the expression portion, but any variable name that represents that field can be used in the def method in the code block.
The concatenation expression can include literal strings and field values in exactly the way you have shown. Python considers single quotes and double quotes to be equivalent (as long as they are used in pairs to open and close the string). So field1 + "-" + field2 + "-" + field3 and field1 + '-' + field2 + '-' + field3 work the same.
As the “VB” is no longer available within the “field calculator” tool, I couldn’t figure out how to write a code for the if statement in Python for the 5 cases shown in the screenshot below
Follow the screenshot below and it will work fine with you
Manty thanks Abdullah for the help. It works fine with me