1

I'm new to Python.

I would like to calculate field values for a specific 'IDF' based on values from another column. I have something like that:

with arcpy.da.UpdateCursor(fc, ['IDF', 'og70_79_sales_sum','SUM_og70_79_103_sales']) as cursor:
for row in cursor:
 if row[0] == 103:
 row[1] = row[2]
 cursor.updateRow(row)
 print('{} - IDF, {} - value, update og70_79_sales_sum: {}'.format(row[0], row[2], row[1]))

but it does not work.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 30, 2018 at 17:53
6
  • 2
    Are you sure the IDF field is numeric? It might be a string causing your condition to be False everytime Commented Mar 30, 2018 at 17:56
  • 1
    Yes, IDF is numeric field (double). Commented Mar 30, 2018 at 17:58
  • When in "row[1] = row[2]" instead of row [2], I enter a value, for example 100 it is ok. Fields 'og70_79_sales_sum' and 'SUM_og70_79_103_sales' are of the same type. Commented Mar 30, 2018 at 18:02
  • 1
    You need to fix your formatting--the for loop block needs to be indented. The cursor.updateRow(row) statement needs to be unindented so that it is directly within the for loop. What is the error you are getting? Commented Mar 30, 2018 at 18:14
  • I'm not getting any error. Fields are still empty. Commented Mar 30, 2018 at 18:34

1 Answer 1

4

You stated in a comment that IDF is a Double field. However, you are comparing it to an integer, so your if statement is probably failing. Try converting the float to an int for the comparison:

if int(round(row[0])) == 103:

As an aside, I would highly recommend storing ID fields as Long/Short/Text, never Float/Double, because comparisons against Float/Double can be extremely unreliable as you see here.

answered Mar 30, 2018 at 18:51
1
  • +1 for never using a float or double field to store an ID value. Commented Mar 30, 2018 at 18:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.