Python script for if/elif condition in field calculator [duplicate]

This question already has an answer here: I need some help in calculating the last column (OD_pairs.Ai_) based on the condition; if travel_cost has a value below 10, OD-pairs.Ai_ takes the value of the first column(Aij_OjFCij..) if travel_cost has a value above 10, then return 0 in OD_pairs.Ai. So basically the third field to be calculated is either the corresponding value in the first field or zero, depending on the value in second field.

marked as duplicate by Mike T, Kevin, Chris W, Evil Genius, PolyGeo♦Oct 12 '15 at 21:32

My solution is in Python, don't forget to switch to Python Parser in the Field Calculator window.

Pre-Logic Script Code:

def calcColumn(cost, Aji):
if cost < 10:
return Aji
elif cost > 10:
return 0

OD_pairs.Ai_ =

calcColumn(!Travel_Cost!, !Aij_OjFCij_Income4!)

Bare in mind if you have cost = 10, it won't work, as it only handles larger than or smaller than 10. You can also instead use:

Pre-Logic Script Code:

def calcColumn(cost, Aji):
if cost < 10:
return Aji
else:
return 0
• Edit: Reread his post. It is confusing, but the code you proposed is correct. – Richard Fairhurst Oct 12 '15 at 17:50
• Alvin if it works please mark as answered. – Nir Oct 12 '15 at 18:56