Is there an easy way to calculate all Null integer fields to 0 in a GDB?

Selecting Null values and field calculating each column to 0 is not a viable option as I need to do this for a few hundred columns across multiple layers.


You can do this for many fields with some Python. This is a stand alone example, but it'd be easy to write this into a script/Python toolbox. Or you could embed this into another loop and do this on a list of feature classes/tables.
Keep in mind this approach will make the changes to all short/long integer fields:

import arcpy

features = 'SomeGDB.gdb/SomeFeatureClass'
fields = arcpy.ListFields(features)
fnames = [x.name for x in fields if x.type in ['Integer','SmallInteger']]

with arcpy.da.UpdateCursor(features, fnames) as rows:
    for row in rows:
        updated = False # keep track if we actually update something
        for v in range(len(fnames)):
            if row[v] is None:
                row[v] = 0
                updated = True
        # only update the row if we changed something
        if updated: rows.updateRow(row)
  • Traceback (most recent call last): File "<string>", line 4, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy_init_.py", line 1138, in ListFields return gp.listFields(dataset, wild_card, field_type) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing_base.py", line 346, in listFields self._gp.ListFields(*gp_fixargs(args, True))) IOError: <unprintable IOError object>
    – Ryan
    Sep 22 '16 at 19:35
  • @Ryan Sounds like it couldn't open that dataset for some reason. It may be open in an instance of arcmap somewhere, etc. Sep 22 '16 at 19:38
  • I'll try to test it with another dataset tonight and see if this works. I'm not fluent in python and this is my first time using Python toolbox. The only thing I should have to change in your povided script is point to my gdb/feature class right? Thanks!
    – Ryan
    Sep 22 '16 at 20:10
  • instead of maintaining updated just use a list comprehension and compare that. newRow = [0 if v is None else v for v in row] and then if newRow == row. The fastest would likely be similar to my answer. Instead of deleting fields, you are casting None to 0 when converting to numpy.
    – Paul
    Sep 22 '16 at 20:22
  • @Ryan Yes, at a minimum. Also check to be sure you have it spelled correctly. Ideally, a Python Toolbox would have parameters so that you could specify it as an input, but there's nothing wrong with starting off simple as you learn. Also, if you are using a Python toolbox, be sure to refresh it or restart ArcGIS after you make changes. Sep 22 '16 at 21:00

Select all Null values from the field (FIELD IS NULL) -> Right click the field -> Field Calculate -> Field = 0

  • I know this is one way but it is slow and I want to avoid it. I need to do this for a few hundred fields on multiple layers.
    – Ryan
    Sep 22 '16 at 18:21
  • @Ryan please update your question with this very relevant information. Tell us what you've tried and why that doesn't work for you.
    – Midavalo
    Sep 22 '16 at 18:42

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.