AnsweredAssumed Answered

UpdateCursor Problem

Question asked by Kinny_K on Feb 5, 2016
Latest reply on Feb 8, 2016 by Dan_Patterson

Disclosure: Portion of my homework

 

The following script creates a count of wells per county and works:

*********************************************************************************************************************

import arcpy

arcpy.overWriteOutput = True

arcpy.env.workspace = "C:\\Users\\kkirkeby\\Desktop\\GIS-4080\\Lesson5_Data\\Lesson5_Data"

 

# Variables

wells = "Wells.shp"

counties = "COUNTIES.shp"

Wells_Intersect = "Wells_Intersect.shp"

Wells_Intersect_Layer = "Wells_Intersect_Layer"

input_f = ["Wells.shp", "COUNTIES.shp"]

 

# Process: Intersect

#arcpy.Intersect_analysis(input_f, Wells_Intersect, "ALL", "", "INPUT")

 

# Process: Make Feature Layer

#arcpy.MakeFeatureLayer_management(Wells_Intersect, Wells_Intersect_Layer)

 

# List County Names

countyList = [row[0] for row in arcpy.da.SearchCursor(counties, "COUNTY")]

 

# Count wells for each county

for cname in countyList:

    whereclause = "{} = '{}'".format("COUNTY", cname)

    wellCnt = 0

    with arcpy.da.SearchCursor(Wells_Intersect_Layer, "COUNTY",whereclause) as cursor:

        for row in cursor:

            wellCnt = wellCnt + 1

        print cname + ", " + str(wellCnt)

*********************************************************************************************************************

I need to update the second to last column in the row with wellCnt. My thought was to modify the script with the following:

 

forcname in countyList:

    whereclause = "{} = '{}'".format("COUNTY", cname)

    wellCnt = 0

    with arcpy.da.UpdateCursor(Wells_Intersect_Layer, "COUNTY",whereclause) as cursor:

        for row in cursor:

            wellCnt = wellCnt + 1

        row[-2] =  wellCnt

        cursor.updateRow(row)

 

Error: IndexError: list assignment index out of range

 

Thanks for any advice or assistance.

Outcomes