0

I want my code to find the maximum value of a field from attribute table and return that as a the value for another field in another attribute table, trying to do this with search cursors and update cursor.

The search cursor seems to work fine, when I print the result is as expected, however, the update cursor does not return this value in the other attribute table but no errors are returned.

I am using ArcGIS Pro v. 3, arcpy with Python 3.

Here is my code:

fields = ["B1a_Score"]
all_rows = [i[0] for i in arcpy.da.SearchCursor(PA_Intersection,fields[0])]
max_val = max(all_rows)

print(max_val) #Correctly returns 4

AU = "AU_am"
field = ["B1a_Score"]

with arcpy.da.UpdateCursor(AU,field) as cursor:
    for row in cursor:
        row[0] = max_val
        cursor.updateRow(row) #B1a_Score in AU is not updated to 4 instead reads <null>

del cursor
6
  • 1
    A CalculateField would be faster (since it's C++). I wonder what would happen if you used cursor.updateRow([max_val])
    – Vince
    Commented Dec 12, 2022 at 17:03
  • 2
    It helps to state which software (ArcGIS Desktop/ArcMap, ArcGIS Pro) and which version. Also, stating the data source is often helpful. Are you working with shape files, file geodatabases, enterprise geodatabases, etc...?
    – bixb0012
    Commented Dec 12, 2022 at 17:07
  • Added an edit, I am using Python 3 within ArcPro v.3 using AU is a shapefile and PA_Intersection is a table within a geodatabase Commented Dec 12, 2022 at 17:40
  • B1a_Score is a number field - LONG, in both instances Commented Dec 12, 2022 at 17:46
  • Yes it does, AU is defined earlier in the script, I don't think I can post the entire script as it's ~ 200 lines Commented Dec 12, 2022 at 17:55

2 Answers 2

1

It looks like your code is correct and it's not clear why the update cursor is not updating the values in the table as expected. There are a few things you can try to diagnose and solve the problem.

First, make sure that the field you are trying to update in the AU table exists and has the correct data type to hold the values you want to update it with. If the field does not exist or has the wrong data type, the update cursor will not be able to update it.

Next, try printing the values of the rows in the AU table before and after you update them using the update cursor. This will help you see if the values are actually being updated or not.

Finally, you can try using the arcpy.CalculateField_management() function instead of a search cursor and update cursor to see if it produces the same results. This function can be used to calculate the maximum value of a field in a table and update another field with that value in a single step.

1
  • Thank you for these suggestions, I had already tried most of these. In the end taking a step back I realised that I could just return max_val at the end of my function to achieve my overall goal of the main script not posted as too long. Commented Dec 13, 2022 at 11:56
0

The code originally sits within a function being called to calculate a field, I've changed to remove the update cursor and just return the max value, this seems to work for my overall purpose of calculating field:

    all_rows = [i[0] for i in arcpy.da.SearchCursor(PA_Intersection,fields[0])]
    max_val = max(all_rows)

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