0

I am doing some data migration and need the number of records with actual data, sans null and blank fields, without switching back and forth with select by attribute.

Ideally I would like to run a script for a table or feature class that gives me the count of each field that has data.

2

You could try something along these lines:

import arcpy
fc = "MY FEATURE CLASS NAME AND PATH"

# Get list of fields
fields = [f.name for f in arcpy.ListFields(fc)]

# Set up dictionary for the fields
countdict = {}
for field in fields:
    countdict[field] = 0

# Loop through Table and see which fields are NULL or Blank
cur = arcpy.SearchCursor(fc)
for row in cur:
    for field in fields:
        if not row.getValue(field) in [None, " ", ""]:
            countdict[field] = countdict[field] + 1

# Print out results - shows field name and number of non NULL/Blank fields
for f in countdict.keys()
   print "%s - %s" % (f, countdict[f])
1

Taking @dklassen snippet and enhancing a little with multiple tables iteration in a .gdb if that is the case. Outputs table name , total records , field name and number of non NULL/BLANK fields.

    from arcpy import env
    import arcpy

    # Set the current workspace
    #
    env.workspace = "your path here"


    # Get the list of tables in the geodatabase 
    # print to interactive window.
    # get total count records + count of fields excluding nulls/blanks

    tableList = arcpy.ListTables()
    tableCount = 0
    for table in tableList:
        tableCount += 1
        count = arcpy.GetCount_management(table)
        print "******* Table - " + table + ". counts " + str(count) + " records. *******"
        # Get list of fields
        fields = [f.name for f in arcpy.ListFields(table)]
        # Set up dictionary for the fields
        countdict = {}
        for field in fields:
            countdict[field] = 0
        # Loop through Table and see which fields are NULL or Blank
        cur = arcpy.SearchCursor(table)
        for row in cur:
            for field in fields:
                if not row.getValue(field) in [None, " ", ""]:
                    countdict[field] = countdict[field] + 1

        # Print out results - shows field name and number of non NULL/Blank fields
        for f in countdict.keys():
           print "%s - %s" % (f, countdict[f])

    print "Total tables counted:  " + str(tableCount)

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.