1

I'm looking for a python script using ArcPy that can convert all values in a shapefile's attribute table to a value of 0 as I will then populate with other values from another source.

I have reviewed Converting case of all values in all fields of table using ArcPy & Python? and Replacing NULL value with Zero in geodatabase table using Python parser of ArcGIS field calculator? but to no avail as they don't account for other possible numerical values. Here is a snippet of the attribute table.

enter image description here

Also, I have no null values in any of the attribute tables. This is how far I was able to go.

import arcpy

fc = r'path'

desc = arcpy.Describe(fc)
fields = desc.fields

for field in fields:
if field.Type == "Number":
    with arcpy.da.UpdateCursor(fc, str(field.name)) as cursor:
        for row in cursor:
              if x is None or Not 0:
                 return 0
              else:
                 return ?   
  • The use of a def statement within an update cursor seems very odd to me. What happens when you run your code snippet? – PolyGeo Jun 24 '16 at 19:19
  • Do you mean all values in one field or all values in every field need to be turned to zero? Please include a picture of a few rows and columns of your input and expected output data. – PolyGeo Jun 24 '16 at 19:23
  • If you are unable to do this on one shapefile yet then it is premature to be asking about how to do it for 50 shapefiles. – PolyGeo Jun 24 '16 at 19:25
  • I removed the def statement, that was an mistake for what I had wanted to show. Also, I would like to set all values in every field in the entire table. I've included an image. I'm trying to do this on one shapefile and then hopefully it will be used for the other 49 or so. – whyzar Jun 24 '16 at 19:30
  • Wouldn't it be easier to just re-create your table with new fields? – PolyGeo Jun 24 '16 at 19:35
2

Here you have a pretty similar solution.

import arcpy

env.workspace = r"D:/datafolder"

shapefiles_list = arcpy.ListFeatureClasses()

for shapefile in shapefiles_list:
    desc = arcpy.Describe(shapefile)
    fields = desc.fields
    fields_to_zero = []
    for i in fields:
        if i.type in ["FLOAT","DOUBLE", "LONG", "SHORT"]:
            fields_to_zero.append(i.name)
    with arcpy.da.UpdateCursor(shapefile, fields_to_zero) as Cursor:
         for row in Cursor:
             for i in range(0, len(fields_to_zero)):
                 row[i] = 0
             Cursor.updateRow(row)
1

I'm not sure why you have to set all the values to zero before populating them from somewhere else, but this should do it.

import arcpy

fc = r'path'

desc = arcpy.Describe(fc)
fields = desc.fields

procFields = []
for field in fields:
    if field.Type in ('Double', 'Single', 'Integer', 'Float'):
        procFields.append(field.Name)

replVal = [0] * len(procFields) 
with arcpy.da.UpdateCursor(fc, procFields) as cursor:
    for row in cursor:
        row = replVal
        cursor.updateRow(row)
  • 2
    It would be one thing if you wanted to change the field values to NaN (not a number) or Null, but zero is still a number. You shouldn't need to do this, just update the field values with your calculated values. – mkennedy Jun 24 '16 at 16:36

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.