Hi @HussainMalik1 ,
See below the script that allows you to get the statistics:
import arcpy
def main():
# read the input featureclass
# fc = arcpy.GetParameterAsText(0)
fc = r'D:\GeoNet\ExtractText\XanderBakker.gdb\Hussain_Data_ConcatenatedFieldValueCount'
# input data field
fld_name = 'ARCHETYPE'
# define how the items relate to the fields
dct_flds = {'Large Multi-Storey Home built < 2007': 'LMSHBLT2007',
'Large Multi-Storey Home built >= 2007': 'LMSHBGE2007',
'Large Multiplex built < 2007': 'LMBLT2007',
'Large Multiplex built >= 2007': 'LMBGE2007',
'Large Retail': 'LargeRetail',
'Large Schools': 'LargeSchools',
'Large Single Storey Home built < 2007': 'LSSHBLT2007',
'Large Single Storey Home built >= 2007': 'LSSHBGE2007',
'Medium Apartments Mixed': 'MediumAptMixed',
'Medium Offices': 'MediumOffices',
'Small Apartments Mixed': 'SmallAptMixed',
'Small Multi-Storey Home built < 2007': 'SMSHMLT2007',
'Small Multi-Storey Home built >= 2007': 'SMSHBGE2007',
'Small Multiplex built < 2007': 'SMBLT2007',
'Small Multiplex built >= 2007': 'SMBGE2007',
'Small Offices': 'SmallOffices',
'Small Retail': 'SmallRetail',
'Small Schools': 'SmallSchools',
'Small Single Storey Home built < 2007': 'SSSHBLT2007',
'Small Single Storey Home built >= 2007': 'SSSHBGE2007',
'UNDEFINED': 'UNDEFINED',
'Very Small Apartments Mixed': 'VerySmallAptMixed'}
# add fields if necessary
addfields = True
if addfields:
AddFields(fc, dct_flds)
# create list of fields
flds = [fld_name]
for fld in dct_flds.values():
flds.append(fld)
# loop through data
with arcpy.da.UpdateCursor(fc, flds) as curs:
for row in curs:
archetype = row[0]
# calculate statistics
dct_cnts = GetStats(archetype, dct_flds)
# set row stats values
for fld_name, cnt in dct_cnts.items():
i = flds.index(fld_name)
row[i] = cnt
# write updated row
curs.updateRow(row)
def AddFields(fc, dct_flds):
# Add the fields mentioned in the dictionary
for description, fld_name in dct_flds.items():
if not FieldExists(fc, fld_name):
arcpy.AddField_management(fc, fld_name, "LONG")
def FieldExists(fc, fld_name):
# Check if field exists
if len(arcpy.ListFields(fc, fld_name)) == 0:
return False
else:
return True
def GetStats(archetype, dct_flds):
# get the stats from the archetype
dct_cnts = {}
archetypes = archetype.split(";")
for description in archetypes:
if description in dct_flds:
fld_name = dct_flds[description]
if fld_name in dct_cnts:
dct_cnts[fld_name] += 1
else:
dct_cnts[fld_name] = 1
else:
# oops, value not defined in dct_flds, report this
print('{} is not included in dct_flds'.format(description))
pass
return dct_cnts
if __name__ == '__main__':
main()
This script points on line 6 to the data you shared. I noticed that the featureclass did not have any output fields defined, so I added code to add the new fields if they don't exist. Please note that there are a large number of UNDEFINED values in the fields.
Below some statistics of what was found in the data:
Description | Count | Output Field name |
Large Multi-Storey Home built < 2007 | 272 | LMSHBLT2007 |
Large Multi-Storey Home built >= 2007 | 160 | LMSHBGE2007 |
Large Multiplex built < 2007 | 1708 | LMBLT2007 |
Large Multiplex built >= 2007 | 1548 | LMBGE2007 |
Large Retail | 60 | LargeRetail |
Large Schools | 12 | LargeSchools |
Large Single Storey Home built < 2007 | 2852 | LSSHBLT2007 |
Large Single Storey Home built >= 2007 | 44 | LSSHBGE2007 |
Medium Apartments Mixed | 40 | MediumAptMixed |
Medium Offices | 8 | MediumOffices |
Small Apartments Mixed | 72 | SmallAptMixed |
Small Multi-Storey Home built < 2007 | 1980 | SMSHMLT2007 |
Small Multi-Storey Home built >= 2007 | 424 | SMSHBGE2007 |
Small Multiplex built < 2007 | 2484 | SMBLT2007 |
Small Multiplex built >= 2007 | 392 | SMBGE2007 |
Small Offices | 20 | SmallOffices |
Small Retail | 132 | SmallRetail |
Small Schools | 28 | SmallSchools |
Small Single Storey Home built < 2007 | 6668 | SSSHBLT2007 |
Small Single Storey Home built >= 2007 | 4 | SSSHBGE2007 |
UNDEFINED | 552 | UNDEFINED |
Very Small Apartments Mixed | 224 | VerySmallAptMixed |
Let me know if you have any questions. I will try to send the FGDB by private message (if it fits)...