Hi - I'm having trouble getting an if statement to work and was hoping someone could help?
I need to copy the data from a field called SUM_Green to G, replace any null values in a dataset with 0, so I've used the following, but can't seem to find anything that works:
Any ideas?
Thanks.
Solved! Go to Solution.
I agree with Darren Wiens that using Python Conditional Expressions is the way to go in situations like this. In case you couldn't rely on SUM_Green being greater than or equal to one, you can explicitly handle NULL by using Python's NoneType:
arcpy.CalculateField_management(MULTIs, "G", "0 if !SUM_Green! is None else !SUM_Green!", "PYTHON_9.3","")
Just slightly different syntax:
arcpy.CalculateField_management(MULTIs, "G", "!SUM_Green! if !SUM_Green! >= 1 else 0", "PYTHON_9.3","")
I agree with Darren Wiens that using Python Conditional Expressions is the way to go in situations like this. In case you couldn't rely on SUM_Green being greater than or equal to one, you can explicitly handle NULL by using Python's NoneType:
arcpy.CalculateField_management(MULTIs, "G", "0 if !SUM_Green! is None else !SUM_Green!", "PYTHON_9.3","")
Thanks for both your responses, I've tried Joshua's suggestion and the script has ran through perfectly, just what I needed. Cheers.