3

I have a working formula that I can put in the Field Calculator which I'd like to use in my python code:

.3-.001*Log ( [Field1]+1 )-.02*Log ( [Field2]+1 )-.04*Log ( [Field3]+1 )

My code:

import math
expression =".3-.001*Log(!Field1!+1)-.02*Log(!Field2!+1)-.04*Log(!Field3!+1)"
arcpy.CalculateField_management("Layer","Field4",expression)

ExecuteError: ERROR 999999: Error executing function. Syntax error Failed to execute (CalculateField).

How can I write this expression?

1
  • expression =".3-.001*math.log(!Field1!+1)-.02*math.log(!Field2!+1)-.04*math.log(!Field3!+1)"
    – Bjorn
    Nov 20, 2017 at 18:51

1 Answer 1

6

You should change "Log" to "math.log" .

If you want to use your code in Field Calculator, just use formula in it:

0.3-.001*math.log(!Field1!+1)-0.02*math.log(!Field2!+1)-0.04*math.log(!Field3!+1)

In Python IDE:

You need to assign "PYTHON_9.3" to the last parameter of the "arcpy.CalculateField_management".

Don't forget import arcpy in the beginning of the code when you use the code in the Python IDE or console.

 import arcpy,math
 expression = '.3-.001*math.log(!Field1!+1)-.02*math.log(!Field2!+1)-.04*math.log(!Field3!+1)'
 arcpy.CalculateField_management("Layer","Field4",expression,"PYTHON_9.3")
4
  • ive always wondered why you need import arcpy for field calculator... its inside arcmap you would think its imported by default (like the arcmap python terminal?)
    – NULL.Dude
    Nov 20, 2017 at 20:14
  • 2
    I supposed you use this code in the python IDE. OK i edited my answer. If you use this code in the field calculator you'll not need to use import arcpy
    – BBG_GIS
    Nov 20, 2017 at 20:16
  • @Geo.Dude, @wetland If you're using this code in the field calculator you wouldn't be calling arcpy.CalculateField_management()
    – Midavalo
    Nov 25, 2017 at 4:11
  • @Midavalo You'r right. I edited the answer. Thanks.
    – BBG_GIS
    Nov 25, 2017 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.