6

I'm working on different project each day. At the end I would like to delete the fields that have zero "0" values in. The fields with the zero's will differ from project to project. So the model should look thru each field and if the field has zero's it must delete the field. In the attached image example I would like to delete DOL_J1 and GYPSUM_1. Because of the amount of project I would like to build a model to automate this.

Will this be possible with ModelBuilder or do I need to use Python? I'm not that familiar with Python and would prefer ModelBuilder.

I have an ArcMap license

enter image description here

4
  • 3
    Do you want to delete a field if all the values are = 0? Or if at least one value =0?
    – GISGe
    Jul 5, 2016 at 8:20
  • If all the values are = 0. (I did not show the whole table. The DOL_j1 field have some values lower down) Jul 5, 2016 at 13:40
  • I don't know how easy this is to do in ModelBuilder, but it sure isn't very hard in Python. Pseudocode: 1 List fields. 2 Search cursor for all fields, store the field names for those who only contains 0. 3 Delete said fields. 4 Save output. It is possible that you can have some help from [ET Geowizards](ian-ko.com) too.
    – Martin
    Jul 7, 2016 at 14:28
  • Getting this to work in Model Builder would probably be pretty tricky--might want to investigate Summary Stats.
    – Paul
    Jul 9, 2016 at 2:03

2 Answers 2

3

Here's a very simple approach that uses numpy and will handle fields with null values (if you don't want to, then change null_value to any other number):

import arcpy

fc = "<my fc>"
# Get all numeric fields that aren't required.
fields = [f.name for f in arcpy.Describe(fc).fields
            if f.type.upper() in ['DOUBLE', 'INTEGER', 'SINGLE', 'SMALLINTEGER']
            and not f.required]

# Convert to numpy array, casting nulls to 0.
arr = arcpy.da.FeatureClassToNumPyArray(fc, fields, null_value=0)

# Find all fields whose sum are 0.
delete = [field for field in fields if not arr[field].sum()]
print("Deleting '{}'".format(", ".join(delete)))
arcpy.DeleteField_management(fc, delete)
3
  • Do I need to change anything on your script? I only changed the fc = "c:\Temp\dbfile.dbf". I get the same error: Runtime error <type 'exceptions.AttributeError'>: 'module' object has no attribute 'da' Jul 11, 2016 at 12:48
  • @ThinusEnslin, you'll need 10.1 SP1+ to access the data access module (da).
    – Paul
    Jul 11, 2016 at 15:51
  • I'm using ArcMap 10.0 SP5 Jul 12, 2016 at 6:10
1

Model builder does not have the functionality you seek, you will need to do this using python.

Below is the code that will do this, obviously you need to edit myTable and the parameter "SmallInteger" to suit your needs

import arcpy
import array
myTable = r"C:\Scratch\fGDB_Scratch.gdb\tblTest"

# Get list of fields
fieldList = [f.name for f in arcpy.ListFields(myTable,"","SmallInteger")]
print "Fields to check: " + str(fieldList)

# Create an array
initializer = [0 for x in range(0,len(fieldList),1)]
total = array.array("f",initializer)
print "total array initialized to: " + str(total)

# Sum the values in the fields and store in array total
with arcpy.da.SearchCursor(myTable,fieldList) as cursor:
    for row in cursor:
        for i in range(0,len(fieldList) -1,1):
            total[i] = total[i] + row[i]
print "total array after summing: " + str(total)

# Delete fields if sum is zero
for i in range(0,len(fieldList),1):
    if total[i] == 0:
        arcpy.DeleteField_management(myTable,fieldList[i])
        print "Deleted: " + fieldList[i]

With those two variables set you can simply drop this code into the python command line window and execute it that way. This code assumes that all rows in your fields have a value, so NULL values will probably blow this code up.

7
  • I wonder, would a sort and checking if first and last values = 0 be faster than a cursor?
    – recurvata
    Jul 8, 2016 at 16:46
  • Possibly, something to try I guess? I have found stepping through a cursor once is often a fast approach.
    – Hornbydd
    Jul 8, 2016 at 21:31
  • 1
    @recurvata summing will usually be faster than sorting, even more so as the list gets larger. Compare python -m timeit -s "import random;r=[random.random() for i in range(10000)]" "sum(r)" with r.sort() or sorted(r).
    – Paul
    Jul 9, 2016 at 2:17
  • @Hornbydd, I edit the myTable and SmallInteger, but I get an error message: Runtime error <type 'exceptions.AttributeError'>: 'module' object has no attribute 'da' Jul 11, 2016 at 12:20
  • What version of Arcgis are you using?
    – Hornbydd
    Jul 11, 2016 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.