0

I have a table that I am joining to calculate several long integer fields, when the join is executed all of the values are joined as NULL that don't have values.

How can I use an Update Cursor to calculate every NULL value to 0 that is in a field that has a long integer type?

How can I expand upon this example to iterate through each field that is not a string(double, long) and change all NULL values to 0?

This only needs to be done to one field; however the script below does not update my NULL values to 0.

import arcpy

distfc = 'aFC'

with arcpy.da.UpdateCursor(distfc, ["FREQUENCY"]) as cursor:
    for row in cursor:
        if row[0] ==  NONE:
            row[0] = 0
            cursor.updateRow(row)
  • 1
    Assuming you have the license to overwrite all values calc them all to 0 before the join and use a join as 'matching only', this will omit your non-joined features. If you can't overwrite all then in python you can use a query like table.field is not null, calculate and then invert to get the non-joined and null values and calc to 0. – Michael Stimson Oct 12 '16 at 23:54
  • 1
    The code you have presented is less than a code snippet that illustrates where you are stuck. For example, your title, body and tags all mention cursors but your code does not. I recommend not thinking about GIS SE as being some sort of online GIS tutor. For your questions to be answered here they should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. – PolyGeo Oct 12 '16 at 23:56
  • 2
    How big is your joined table? Instead of joining you could create a dictionary, cursor through your joined table populating the dictionary, then for your calculation and then use if key in dict: to calculate matching rows else calculate 0... works simpler (and faster) than trying to use a join on a cursor. Note: Key is your join field value and value is a tuple or list containing the important row values. – Michael Stimson Oct 13 '16 at 0:07
  • 2
    You mention using an update cursor, but your example doesn't show the cursor. Can you edit your question to include what you've tried using the Update Cursor, and what it actually does when you try it? – Midavalo Oct 13 '16 at 1:42
  • 2
    Change if row[0] == 'NULL': to if row[0] is None: – BERA Oct 13 '16 at 17:08
3

Python is case sensitive--therefore, you need to replace NONE with None (a reserved word in Python)

import arcpy

distfc = 'aFC'

with arcpy.da.UpdateCursor(distfc, ["FREQUENCY"]) as cursor:
    for row in cursor:
        if row[0] ==  None:
            row[0] = 0
            cursor.updateRow(row)

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.