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.
1 Answer 1
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.
-
+1 for never using a float or double field to store an ID value.Dan C– Dan C2018年03月30日 18:55:13 +00:00Commented Mar 30, 2018 at 18:55
IDF
field is numeric? It might be a string causing your condition to beFalse
everytimefor
loop block needs to be indented. Thecursor.updateRow(row)
statement needs to be unindented so that it is directly within thefor
loop. What is the error you are getting?