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

df = arcpy.mapping.ListDataFrames(mxd, "")[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"LV UG Electric Line Segment":
       flds = [ for f in arcpy.ListFields(lyr)]
      s = set(flds)
      if 'SubtypeCD' in s :
         CountUnique = {}
         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
                  CountUnique[desc_Domain[row[0]]] += 1                  

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