AnsweredAssumed Answered

Count and sum unique values

Question asked by sibghatullah86 on Oct 11, 2017
Latest reply on Oct 13, 2017 by bixb0012

I am writing a code for count and sum unique values and exporting results to csv. I want that to be done in a quick way.I am getting the unique values count on Field "SubtypeCD" and exporting them to csv using following code,but i need sum also on shapelength().Please guide 

 

import arcpy
import csv
import os
import io
from arcpy import env
env.overwriteOutput = True

mxd=arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
count=0
field = ['SubtypeCD']

with open(r'D:\result.csv','wb') as resultFile:
wr = csv.writer(resultFile, dialect='excel')
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
   if lyr.name=="LV UG Electric Line Segment":
       flds = [f.name for f in arcpy.ListFields(lyr)]
      s = set(flds)
      print lyr.name
      if 'SubtypeCD' in s :
          wr.writerow([lyr.name])
         CountUnique = {}
          sumUnique={}
         desc_Domain = {key: value['Name'] for (key, value) in arcpy.da.ListSubtypes(arcpy.Describe(lyr).catalogPath).iteritems()}

         with arcpy.da.SearchCursor (lyr,[ "SubtypeCD","Shape.STLength()"]) as cursor:
              for row in cursor:
               if not row[0] in CountUnique.keys():
                  CountUnique[desc_Domain[row[0]]] = 1
               else:
                  CountUnique[desc_Domain[row[0]]] += 1                  
                    sumUnique+=row[1]

for key in CountUnique.keys():
print str(key) + ":", CountUnique[key], "features"
tr(key) ,CountUnique[key],"Features"
wr.writerow(total)

Outcomes