I'm trying to calculate a field (using idle python to write my scipt and working with ArcMap 10.3) by dividing two fields then dividing by a variable that holds a number, which the number was calculated by dividing 2 sums.

So essentially I'm trying to divide two ratios but I'm having trouble going about that. Basically the equation is ( a / b ) / ( sum(x) / sum(y)).

I have the variable set to hold the number of the second ratio of (sum(x) / sum(y)) but can't figure out how to incorporate the variable into the calculate field expression or how to set up a double division in the expression. As you can see I used TableToNumPyArray to figure out the sum ratio.

field = arcpy.da.TableToNumPyArray (fc, ["E_Total", "E__Black"], skip_nulls=True)
LQC = 1.0 *(field["E__Black"].sum()) / (field["E_Total"].sum())
#Fields are in long format, 1.0* forces it to be in float format and this section of the code works!

field = arcpy.da.TableToNumPyArray (fc, ["E_Total", "E__Black"], skip_nulls=True)
Lratio = 1.0 *(field["E__Black"]) / (field["E_Total"])
expression = '(!'+Lratio+'!/!'+LQC+'!)'
arcpy.CalculateField_management(lyr, "LQ_Black", expression, "PYTHON")
  • essentially for the fields and the equation it would more be like ( field A / field B ) / ( sum (field A) / sum (field B)) and I want to populate the new field with the outcome of dividing by both ratios
    – J_Worl
    Aug 9 '16 at 15:53
  • Please post the code directly in your question--not as a picture. You'll need to remember to format your code.
    – Tom
    Aug 9 '16 at 15:54
  • hi, i formatted the code on here the best I can, this is my first time asking a question on here
    – J_Worl
    Aug 9 '16 at 16:00

I don't think your code is doing quite what you think. In CalculateField_management, surrounding a name with ! indicates a field. But as you've set it up here, Lratio and LQC both are holding float values. So, your expression is going to end up looking like (!1.0!/!32.6!), which is probably not what you want. Since you've already calculated these values, just insert the result of the calculation as the expression, i.e.

arcpy.CalculateField_management(lyr, "LQ_Black", "{}".format(Lratio / LQC), "PYTHON")

If you have a third variable that you want to divide by, for example, var3, then

arcpy.CalculateField_management(lyr, "LQ_Black", "{}".format(Lratio / LQC / var3), "PYTHON")

If you have two fields in lyr that you want to divide by the value of LQC, you could write your expression as

arcpy.CalculateField_management(lyr, "LQ_Black", "!field1! / !field2! / {}".format(LQC), "PYTHON")
  • How would I have to write it if I wanted the expression to be field/field/LQC? Do i not need anything extra around the variable?
    – J_Worl
    Aug 9 '16 at 17:37
  • Edited to address your question
    – JCVanHamme
    Aug 10 '16 at 14:51
  • That worked perfect! Thank you so much for your insight
    – J_Worl
    Aug 10 '16 at 15:04

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.