How to append summary statistics to dbf table

1561
6
12-20-2017 02:33 PM
Myla-RaeBaldwin
New Contributor II

I am working on a project to characterize watersheds where we are carrying out fieldwork. I am trying to use Summary Statistics to sum all the stream length channels contained in a watershed for multiple feature classes (drainline).  Using arcpy.Statistics_analysis creates the output I want in a table but my script overwrites the table instead of adding the sum outputs to the table for each drainline feature. I am unable to figure out how to do this. My script on a subset of the data is below:

# Set local variables
ws_drainlines = arcpy.ListFeatureClasses("", "Line", "")
outStatsTable = "C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity"
field = "Watershed"

# Sum the drainline lengths for each DrainLine feature
for drainline in ws_drainlines:
    print(drainline)
    arcpy.AddField_management(drainline, field, "TEXT", "", "", "20")
    arcpy.CalculateField_management(drainline, field, '"' + drainline + '"', "")
    arcpy.Statistics_analysis(drainline, outStatsTable, [["Shape_Length", "SUM"]], field)
   
       
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

Do you want to join the results? http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/add-join.htm or create a new table?

0 Kudos
Myla-RaeBaldwin
New Contributor II

I want to create one table containing the "SUM" results for all the drainline feature classes. My full data set has 60 different feature classes, here I am running the script on only two feature classes. When this script runs it creates a table of results for the first feature class in the list (BVH_DrainLine) and then overwrites that with the last feature class in the list (VRM_DrainLine. My table result is below, with only one row containing only the last feature class, but I want it to contain both.

0 Kudos
DarrenWiens2
MVP Honored Contributor

You can either:

a.) Calculate a unique ID to differentiate your lines in each feature class, Merge together, and calculate all the stats at once.

b.) Change the output name of your stats table each time through the loop so it doesn't get overwritten, and Merge all those together afterwards.

edit: I suppose you could also run Append each time through the loop to add to the stats table. General rule of thumb is to try not to run a tool each time through a loop, but it may be okay for just a few...

Myla-RaeBaldwin
New Contributor II

Thanks Darren,

I tried to save each stats table with the drainline feature class that the stats was run on but I just kept overwriting the table. My workaround was to create a dictionary from the output tables and print the results in the Python Shell, which works for me for now. I put the following code after the last line of the code I first posted.

# Create dictionary
d = {r[0]: r[1] for r in arcpy.da.SearchCursor(outStatsTable, fields)}

for key, val in d.items():
     print key,": ", val

0 Kudos
DarrenWiens2
MVP Honored Contributor

Sure, there are lots of ways to do it. I was thinking just changing the output table name each time through, like below. Of course, you could substitute in your value for 'drainline' or whatever is most useful. Just so you realize the reason your table keeps getting overwritten is that if the name doesn't change, it overwrites the previous file.

outStatsTable = "C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity"

for i in range(10): # numbers 0-9
    print outStatsTable + str(i)

C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity0
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity1
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity2
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity3
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity4
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity5
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity6
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity7
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity8
C:/_LOCALdata/AB_ARCHYDRO/AB_ARCHYDRO_NOP.GDB/DrainageDensity9‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Myla-RaeBaldwin
New Contributor II

Thanks Darren! I figured out how to name each table with the name of the drainline so that the tables all had unique names and were not overwritten. Then I created a list of all the tables and used the Merge_management to merge all of them into one master table. Works perfectly and is exactly what I needed. 

0 Kudos