I am using an ArcPy Search Cursor to return the rows of a specified field within a shapefile.
The for loop iterates through the feature class and returns the value of the rows in a formatted string as show below.
Code:
fields = ['crops_2020','acreage']
fc =r'C:\geodata\Agricultural_Land_Use_-_2020_Update.shp'
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
print("The crop {0} had {1:,} acres gown in 2020".format(row[0],row[1]))
Output:
Question: As of now my code just returns each row of the field 'crop_2020' and the associated acreage value. So if there is 20 'tropical fruit' rows for example, the script prints out this formatted text 20 times with a different acreage for each instance of the feature. What I am trying to figure out is how to return a formatted text string like the one above that returns the Total or Sum of the acreage per each type of crop. The types of crops are stored within the 'crops_2020' field and the acre amounts are stored in the 'acreage' field.
for
loop. Note that, despite the mention of a WHERE clause in the title, your code does not include one.