0

I am trying to calculate the SUM value for (VD_SUM) Based on Basin ID and output in the field "VD_SUM_SUM". I am not sure how to write the SUM part at the end.

This is what I have so far.

import arcpy

with arcpy.da.UpdateCursor('Streams_Geology_R1', ["BASIN_ID", "VD_SUM", "VD_SUM_SUM"]) as cursor:
    for row in cursor:
        row[2] = row[1].sum???? based on basin ID 
        cursor.updateRow(row)
2
  • 4
    Use summary statistics then join output to your table.
    – BERA
    Jun 9 '20 at 7:08
  • 2
    Can you add a screenshot and example of what results you want?
    – BERA
    Jun 9 '20 at 7:18
1

It sounds like you want to sum all values by basin ID. Use a dictionary. Use the basin ID as your key. Add values to your dictionary in an initial iteration of your table. Then iterate your table again and update your table.

import arcpy

#dictionary for storing values
di = {}
#iterate table
with arcpy.da.SearchCursor('Streams_Geology_R1', ["BASIN_ID", "VD_SUM", "VD_SUM_SUM"]) as cursor:
    for row in cursor:
        #get basin id
        bid = row [0]
        #get value
        vdsum = row [1]
        #try adding value to existing key indictionary
        try: di [bid] += vdsum
        #add key and value to dictionary
        except KeyError: di [bid] = vdsum

#update table
with arcpy.da.UpdateCursor ('Streams_Geology_R1', ["BASIN_ID", "VD_SUM_SUM"]) as cursor:
    for row in cursor:
        #get sum
        row [1] = di [row [0]]
        #update row
        cursor.updateRow (row)
2
  • Great thank you it works! I dont understrand all of the code however. What does try: di [bid] += vdum mean? & except Keyerror: di [bid] = vdsum? Is this the part that does the Summing? Also on update curser what does the di part mean?
    – Vanessa
    Jun 10 '20 at 4:27
  • Sorry that reads bad, basically which part in that script actually tells it to Sum the VD_SUM based on Basin ID?
    – Vanessa
    Jun 10 '20 at 6:21

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.