0

I am trying to update a field in an attribute table in a feature Layer with arcpy.da.UpdateCursor. I am running the Python script as part of a model in ArcGIS 10.6.

But I am lacking some of the basic concept of how arcpy.da.UpdateCursor edits the attribute table of my feature layer.

My Python script is based on https://community.esri.com/thread/237881-how-to-calculate-a-field-using-other-field:

import arcpy
import os
# function returns list of unique values in a given field of a table
def unique_values(table , field):
 with arcpy.da.SearchCursor(table, [field]) as cursor:
 return sorted({row[0] for row in cursor})
fc = os.path.abspath(arcpy.GetParameterAsText(0)) 
lst = unique_values(fc, 'NetworkID')
with arcpy.da.UpdateCursor(fc, ['NetworkID', 'outlet']) as cursor:
 for row in cursor:
 row[1] = lst.index(row[0])
 cursor.updateRow(row)

The field I want to edit is called 'outlet' and mostly consists of NULLs and it should be edited based on the value in 'NetworkID'. NetworkID has no NULLS and categorizes the outlets into groups (lets call them networkgroups).

How do I get all values in outlet of the same networkgroup to have a value based on the only not null value in outlet in the same networkgroup?

For example:

outlet NetworkID
null 1
null 1
null 1
S_21 1 #<- as this is the only non NULL in the networkgroup '1' I want to use it to define the other outlet in the same networkgroup '1'
null 2
S_45 2
... ...

should look like:

outlet NetworkID
S_21 1
S_21 1
S_21 1
S_21 1
S_45 2
S_45 2
... ...
asked Jul 17, 2020 at 11:37
5
  • 2
    Welcome to GIS SE. As a new user, please take the Tour, which emphasizes the importance of asking One question per Question. Questions with bulleted lists of questions are likely to be closed as unfocused. Commented Jul 17, 2020 at 12:00
  • @BERA I thought that the problem is a combination of wrong code and wrong model integration. But I admit that it is better to get the code running first before integrating it into the Model. I edited it now to be more focused (and more description) Commented Jul 21, 2020 at 6:50
  • @Vince will be the question opened again? Commented Jul 21, 2020 at 6:54
  • @BERA all my rows have the attribute networkID already assigned. I only use it to group the variable Name_1 (as a grouping variable). I then want to take the only non NULL value in Name_1 to be given to all Name_1 of the same NetworkIDgroup. I don't want to change the values of NetworkID I only want to use it as a grouping variable. Commented Jul 21, 2020 at 14:58
  • @BERA I made it a bit clearer now. But basically S21 is already assigned the networkID 1 (see the first table) I only want to use NetworkID as a grouping variable. Commented Jul 21, 2020 at 15:16

1 Answer 1

1

If you have a table like your first example then use a dictionary to fix it:

import arcpy
fc = r'C:\somedb.gdb\somefeatureclass'
d = {nid:outlet for nid,outlet in arcpy.da.SearchCursor(in_table=fc, field_names=['NetworkID', 'outlet'], where_clause="""{0} is not None""".format(datasource=fc,field='NetworkID'))}
with arcpy.da.UpdateCursor(fc,['NetworkID', 'outlet']) as cursor:
 for row in cursor:
 if row[0] in d:
 row[1] = d[row[1]]
 cursor.updateRow(row)
answered Jul 22, 2020 at 6:46
1
  • Thanks for the answer. When I run line 2 (d = ...) in arcgis python it throws the error: Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> IndexError: tuple index out of range What can that be caused by? Commented Jul 23, 2020 at 8:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.