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. Commented Jun 9, 2020 at 7:08
  • 2
    Can you add a screenshot and example of what results you want? Commented Jun 9, 2020 at 7:18

1 Answer 1

2

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? Commented Jun 10, 2020 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? Commented Jun 10, 2020 at 6:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.