Thanks a lot for the tip. It worked for me. Easy and neat.
Hello,
I would like to use python to create a cumulative sum so that I end up with a table that looks like:
1976 6
1978 2
1979 5
1980 13
# import the module, arcpy...
import arcpy
# Set the current workspace environment variable.
arcpy.env.workspace = r'C:\Documents and Settings\whitley-wayne\My Documents\ArcGIS\Default.gdb'
# I set up a 'mock' table representing the summary output table,
# called 'SummaryStatsOutput'. Set the table variable 'tbl' to that...
tbl = 'SummaryStatsOutput'
# Define another variable 'accumVal' to represent accumulated ACREAGE.
# Initiate it to zero (0).
accumVal = 0
# Establish the 'rows' cursor variable on tbl, which takes 5 parameters -
# The 3 middle ones are 'blank'...
# The last one is the sort parameter - sorting by YEAR...
# (since my mock table was not in order).
rows = arcpy.UpdateCursor(tbl, '', '', '', 'YEAR A')
# looping on 'row' objects contained in the 'rows' cursor object...
for row in rows:
# Adding the current 'getValue' fetch from the ACREAGE field to accumVal.
# (This is '0' for 1st record).
accumVal = accumVal + row.getValue('ACREAGE')
# Setting the ACREAGE field to the 'new' accumVal.
# (a new field wasn't necessary)
row.setValue('ACREAGE', accumVal)
# Updating the row object within the cursor object
# (committing the changes)
rows.updateRow(row)
# Outside the loop, delete the objects to remove lock reference on table
del row, rows
plt.figure() #...plotting code here plt.savefig(out) plt.close()
myTable = r"C:\test.gdb\my_table"
summaryDict = {}
searchRows = arcpy.da.SearchCursor(myTable, ["YEAR","ACRES"])
for searchRow in searchRows:
yearValue, acresValue = searchRow
if yearValue in summaryDict:
summaryDict[yearValue] = summaryDict[yearValue] + acresValue
else:
summaryDict[yearValue] = acresValue
yearKeys = summaryDict.keys()
yearKeys.sort() #sort the years in ascending order
#Print some output
for yearValue in yearKeys:
print str(yearValue) + " = " + str(summaryDict[yearValue]) + " acres"
Great stuff thanks! I'm hoping to use this to create creaming curves for oil and gas exploration. What I have noticed is that it creates totals of a value per yer but I think the main aim was to create cumulative totals. As an example I used this code on my data:
2011 = 35.0 mmboe
2012 = 616.666667 mmboe
2013 = 389.166667 mmboe
2014 = 348.333333 mmboe
2015 = 107.5 mmboe
2016 = 433.333333 mmboe
Of which is correct as it creates totals for each year. Is it possible to create a cumulative total of this so each previous year is added to the total of the following year? I then want to plot cumulative curves using matplotlib.