0

I figured out that I can use field calculator on a single column in ArcGIS 10.3 attribute window as follows:

def RemoveNULL(x):
    if x is None:
        return ''
    elif x == '':
        return '0'
    else: return x

RemoveNULL(str(!myxField!))

What I really want to do is find a way to find all the null records for all fields in a shapefile that are NULL (truly empty or '') and change them to zero but preserve all other values (double or int numeric). I don't care about text.

  • 2
    Shapefiles, based as they are on dBase-III+, don't support the concept of numeric nulls. Please edit the question to include the GIS software in use. – Vince Jul 2 '15 at 4:14
  • 1
    I concur with Vince, if you see NULL in a field it's a string of 4 characters and not DBNull.Value/None/NULL (depending on programming/scripting language). – Michael Stimson Jul 2 '15 at 4:21
  • arcGIS desktop 10.3 built in python field calculator, thanks for looking at this. – prometheus Jul 2 '15 at 4:23
1

I really like using feature layers and selection for this kind of thing. Below I find all my relevant fields, then try selecting Nulls/''. If there's a selection, I field calculate.

I haven't tested the script, so it may not be perfect.

def removeNumericNulls (inFeatureClass):
    import arcpy

    #Relevant field types
    fieldTypes = ["Double",
                  "Single",
                  "Integer",
                  "SmallInteger"]

    #Get fields
    fields = []
    for fieldType in fieldTypes:
        fields += [f.name for f in
                   arcpy.ListFields (inFeatureClass, field_type = fieldType)]
    #Feature layer to allow selection
    arcpy.MakeFeatureLayer_management (inFeatureClass, "lyr")

    for field in fields:
        #SQL to select nulls
        fieldDelim = arcpy.AddFieldDelimiters ("lyr", field)
        sql = "{} IS NULL OR {} = ''".format (fieldDelim)

        #Select nulls
        arcpy.SelectLayerByAttribute_management ("lyr", "", sql)

        #Skip if no selection/no null values
        if not arcpy.Describe ("lyr").FIDSet:
            continue

        #Calc field
        arcpy.CalculateField_management ("lyr", field, "0")

    arcpy.Delete_management ("lyr")
  • are you running this from python.exe and calling the .mxd or are you running this inside arcgis through python? – prometheus Jul 2 '15 at 13:50
  • You can copy/paste this into the Python window in ArcGIS, add the layer to the table of contents you wish to scrub, and then in the Python window: removeNumericNulls ({lyr}) where {lyr} is the name of the layer in your table of contents. Or you can find IDLE (Start -> All Programs -> ArcGIS -> Python 2.7 -> IDLE (Python GUI)). In the python shell paste the function. Then you can use the full path of any feature class or table for the function's input. Such as: removeNumericNulls (r"C:\temp\temp.gdb\example"). The full path is easiest to copy from ArcCatalog. Don't forget the r. – Emil Brundage Jul 2 '15 at 15:59
  • 1
    Still the fact remains that NULL is not part of the DBASE(IV) spec see gis.stackexchange.com/questions/28411/… (answer by whuber) and especially resources.arcgis.com/en/help/main/10.1/index.html#//… in When should I not use a shapefile? there shouldn't be any NULL values in the table to select... it would be different if it were a geodatabase feature class. – Michael Stimson Jul 3 '15 at 1:18
  • 1
    @MichaelMiles-Stimson sometimes you gotta go with the spirit of the question, not the letter of the question. I suspect poster uses the term shapefile for other spatial data types. I hear it used by my fellow GIS professions improperly all the time. – Emil Brundage Jul 3 '15 at 1:24
  • That's new to me, though it doesn't sound inconceivable that the terms shapefile and feature class become interchangeable thereby referring to a feature class as a shapefile... stranger things have happened by pseudo-GIS users (those that need to understand about 1% of GIS to perform their tasks but are trying hard to say the right things). – Michael Stimson Jul 3 '15 at 1:31

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.