My python script calculates only the value of the immediate previous record instead of calculating the values of all preceding records before that particular record. The example (the image) below is the undesired output;
While the image below here is what i want to achieve;
This is my python script i used;
import arcpy from arcpy import da CensusBldForm = "CensusBldForm" Calculated_EA1=0 with arcpy.da.UpdateCursor(CensusBldForm, ["OBJECTID", "EstimatedPopulationinEA", "Calculated_EA"]) as cursor: for row in cursor: Calculated_EA2 = row[1] row[2] = (Calculated_EA1 + Calculated_EA2) Calculated_EA1 = Calculated_EA2 cursor.updateRow(row)
Kindly assist me with the above please.
Thank you very much for your response, can you kindly relate the script above to feature layers instead as i can't really relate them as needed. Thanks
Can you use Python and create a gp tool? that is, do you have access to ArcGIS Server? I am not at a location that I can write /test Python script right now, but I had a similar need to looping thru records, and resetting a value every so often. In my case, I used a cursor, and as the value of a field changed, my value incremented. Once it hit 10, it would reset to one and start again.
so using a cursor and a variable to keep a running sum of your values...a quick test after each row in the cursor, reset as necessary. Of course, this assumes one time need....not real time as records are added, since unless you add a field, and store the sum in the field (which could be totally legit, if it helps).
a snippet of my code...althoug not what you are needing, might help demonstrate (posting...then will edit to format) edited on iPad...may not be quite right on indentation (sorry)
displayTrancsCnt = 1 displayID = 1 # The next lines are within a cursor, use counters and sum with tests displayTransCnt += 1 # increments to count 10 trans for each group if displayTransCnt == 11: # once count hits 11... displayTransCnt = 1 # ..reset the count displayID += 1 # ..increment the ID for next set of 10 displayGrpCnt += 1 # ..increment the sets of 10, before repeating if displayGrpCnt == 11: # once Grp hits 10 sets.., displayGrpCnt = 1 # ..resets to grp count to 1 again displayID = 1 # ..resets the ID to 1 again
Excel wont work as we intend to use the form in ArcGIS Online?
The first part of your request, if I understand it, seems straightforward enough, but I don't get the threshold condition. You state if the population values in the rows becomes 500 (do you mean exactly 500?), then the next row's value is either zero or some difference. Well, which is it, zero or the difference? Can you present an example like the one above but showing the 500 threshold value in action?
It's such a bummer when the quickest way is to not use Python, but easier is easier... I agree with Dan.
the quickest way would be to use a spreadsheet since you can copy the formula as you need it. Since you don't seem to be doing thousands or rows nor have you indicated that this would be a daily calculation, it may be faster in the long run to set up a template spreadsheet, then bring it into Arc* for further work.
Thanks for your reponse(s), what i want to achieve is to get the sum total of values from a field called population, but i really don't want the final sum, what i need is to get the sum as you loop through the rows (that's the population value) i.e if
row 1 = 23 (row 1 would be 23 as there's no other row/record before row1)
row 2 = 30 (row 2 would be 30 + the value in row 1)
row 3 = 22 (row 3 would be 22 + the value in row 2), till you reach the last row.
I also want to add a constraint whereby if the sum of the population values in the rows becomes 500, the next row's value would start from zero or the difference that makes the 500 from the previous row. Thanks
Waiting for clarification ... but that is not in our hands
It looks like clarity is needed on both of these threads. Is there a way to combine both of them in order to consolidate and alleviate confusion?
previous link and advice can be found here if anyone is looking for background I need to automatically calculate a field based on values from previous rows
Could you expand on your example so we may get a better understanding of what you are trying to do?
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.