Select to view content in your preferred language

Python if statement in arcpy.CalculateField_management isn't working?

1901
3
Jump to solution
12-12-2016 09:29 AM
MichaelRamm
New Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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","")

View solution in original post

3 Replies
DarrenWiens2
MVP Honored Contributor

Just slightly different syntax:

arcpy.CalculateField_management(MULTIs, "G", "!SUM_Green! if !SUM_Green! >= 1 else 0", "PYTHON_9.3","")‍‍
JoshuaBixby
MVP Esteemed Contributor

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","")
MichaelRamm
New Contributor

Thanks for both your responses, I've tried Joshua's suggestion and the script has ran through perfectly, just what I needed.  Cheers.

0 Kudos