AnswerBun.com

Finding if field has empty records using ArcPy

Geographic Information Systems Asked by Arb on December 7, 2020

I am trying to iterate all fields in a feature class/shape file to check for empty values. If there are empty records in any field, I would like to print that field.

I know Python a little bit and here is my attempt.

import arcpy
from arcpy import env

env.workspace = r"C:SOFTWAREPythonJun2019"
fc ="test.shp"
##
fieldlist = arcpy.ListFields(fc)
##Iterate feilds
for field in fieldlist:
           # Iterate over each feature; if it has a non-null value
           with arcpy.da.SearchCursor(fc, field) as cursor:
               for row in cursor:
                   if row[0]h=

I am stuck here.

How do I continue?

enter image description here

I want print fields TYPE,CAT,SUBTYPE etc. which are having empty values

2 Answers

Here are two approaches. I suspect the first approach is faster as it breaks out of the loop as soon as it finds a value in a search list:

Approach 1: Classic Search Cursor

import arcpy

fc = r'C:pathtoyourgeodatabase.gdbfeatureclass'

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

bad_list = []

for field in fields:
    with arcpy.da.SearchCursor(shp, field) as cursor:
        for row in cursor:
            if row[0] in ["", None, " "]:
                bad_list.append(field)
                break

print bad_list

Approach 2: Query Dictionary

Here is another approach that queries a dictionary populated by a Search Cursor.

Use a list comprehension to get a list of all the fields in the featureclass

fields = [x.name for x in arcpy.ListFields(fc)] 

Use a Search Cursor to populate a dictionary with unique values (set()).

d = {field: set(x[0] for x in arcpy.da.SearchCursor(fc, field)) for field in fields}

The resulting dictionary has the following structure:

{'Field1': ['A','B','C'], 'Field2': ['A', None, 'B'], 'Field3': [1, 2, 3]}

Now query the dictionary with the following search list [None, "", " "] and populate a list with the fields that contain items in the search list:

bad_list = [a for a, b in d.items() if any(w in b for w in [None, "", " "])]

Putting it all together:

import arcpy

fc = r'C:pathtoyourgeodatabase.gdbfeatureclass'

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

# Populate a dictionary where key = field and value = list of unique values
d = {field: set(x[0] for x in arcpy.da.SearchCursor(fc, field)) for field in fields}

# Query dictionary and return list of fields that contain an item in the search list
bad_list = [a for a, b in d.items() if any(w in b for w in [None, "", " "])]

Correct answer by Aaron on December 7, 2020

You could identify all of the rows where any of those fields are empty by including a where clause in your cursor:

with arcpy.da.SearchCursor(fc, field,'"TYPE" IS NULL or "SUBTYPE" IS NULL or "CAT" IS NULL') as cursor:
    for row in cursor: #only includes rows where at least one of the three are empty

The SQL query will vary depending on the dataset type and field types (The example above is looking for null values in a FGDB feature class).

Then you will only parse the table once.

Answered by Bjorn on December 7, 2020

Add your own answers!

Related Questions

Contour Lines exported as raster in Illustrator

1  Asked on July 17, 2021 by rheesuspieces

     

Clipping ENVI files loses information in header (.hdr) file

0  Asked on July 17, 2021 by fernando-romero-galvan

       

Adding values into new fields with PyQGIS

1  Asked on July 16, 2021 by juanjo-valero

   

Getting R to recognize buffer around las file

1  Asked on July 16, 2021 by jessieelliott

       

White sides at marker popup

1  Asked on July 16, 2021 by organic88

     

What is priority in mapconfig_for_cars.xml of osm2pgrouting?

1  Asked on July 16, 2021 by bimo-aji-widyantoro

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir