This is a very simple task to do but I can't understand the right syntax.

I have a shapefile whose attributes are similar to the following:

0   Polygon 0      1      0
1   Polygon 3      0      7
2   Polygon 3      4      7

The number of fields and their names are always different.

I need to create a new field (let's name it NUM), and count the number of zeros in each row.

Example output:

0   Polygon 0      1      0      2
1   Polygon 3      0      7      1
2   Polygon 3      4      7      0

I know how to create a new field, however I am not clear on the next steps.

The working code:

#path is path to shape file
def a(path):
  fields = arcpy.ListFields(path,"FID_*") #FID_* is wildcard to select a fields name 
  arcpy.AddField_management(path, "NUM", "SHORT") #create a field with name NUM
  cursor= arcpy.UpdateCursor(path) 
  for row in cursor:
    for field in fields: 
      a= row.getValue(field.name) #take a value 
      if a==0: #if value=0 then value=value+1
    row.setValue("NUM", count)
  del row 
  del cursor

Thanks blah238, now I can eat pythons!


Here's an outline of one way to do this. I'll leave the coding to you as an exercise.

  1. Read the parameter(s) you need, such as the input table
  2. Create a fields variable using ListFields(), optionally passing in the expected field_type, e.g. Long
  3. Add a new field, "NUM", to the table using AddField()
  4. Create a cursor variable using UpdateCursor()
  5. For each row in cursor:
    • Initalize a count variable
    • For each field in fields:
      • Get the value of field using row.getValue()
      • If value equals 0, increment count by 1
    • Set the value of the added field to count using row.setValue()
    • Call cursor.updateRow(), passing in the current row
  6. Delete the row and cursor variables

To do this you could open an UpdateCursor and run through each row using code (untested) like this.

rows = arcpy.UpdateCursor(path)   # path is your shapefile
for row in rows:
  count = 0
  if row.field1 == 0:
    count = count + 1
  if row.field2 == 0:
    count = count + 1
  if row.field3 == 0:
    count = count + 1
  row.num = count

If you have field1 to fieldN then you will need to also loop through the fields and use row.getValue() and row.setValue() to work with variable rather than hardwired field names.


Perhaps use my Answer as hints to complete @blah238's exercise :-)

  • 2
    I like the more "Pythonic" expression of: count+=1 instead of count = count + 1 but that's just me. – Fezter Oct 10 '12 at 5:41
  • blah238 is torturing me!!! – Vasiya Oct 10 '12 at 5:52
  • I aspire to be more "Pythonic" so will try to remember for next time - which is what I intended last time I saw that syntax - thanks! – PolyGeo Oct 10 '12 at 5:53
  • @Vasiya sorry about that, but I'm sure you've heard the proverb "Give a man a fish and he eats for a day, teach a man to fish and he eats for a lifetime" :) – blah238 Oct 10 '12 at 5:56
  • @blah238 The problem is that i am herbivore and don't eat pythons!!! =) – Vasiya Oct 10 '12 at 6:03

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.