1

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:

enter image description here

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.

2
  • 3
    Check out the 'Summary Statistics' tool (or arcpy equivalent). pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/… Commented Jul 28, 2022 at 23:00
  • This is, in essence, a question on how to use Python. The cursor returns a list of lists. All you need to do is manage some dictionaries inside the for loop. Note that, despite the mention of a WHERE clause in the title, your code does not include one.
    – Vince
    Commented Jul 28, 2022 at 23:17

2 Answers 2

3

Similar to @timlohnes answer, but using a dictionary for all crop types, instead of a series of hard-coded scalar variables for each crop type.

cropDict = {}

with arcpy.da.SearchCursor(fc,fields) as cursor:
  for row in cursor:
    if row[0] in cropDict:
      cropDict[row[0]] += row[1]
    else:
      cropDict[row[0]] = row[1]

for crop in cropDict:
  print("The crop {} had {} acres grown in 2020".format(crop, cropDict[crop]))

(Untested - written off the top of my head.)

Or as @bix0012 commented, you could make it even simpler by using a defaultdict instead of a simple dict:

cropDict = defaultdict(float)

with arcpy.da.SearchCursor(fc,fields) as cursor:
  for row in cursor:
    cropDict[row[0]] += row[1]

for crop in cropDict:
  print("The crop {} had {} acres grown in 2020".format(crop, cropDict[crop]))

(I've replaced "gown" with "grown", assuming it was a typo.)

2
  • It worked! Thank you a bunch!!
    – Ryan Mags
    Commented Jul 29, 2022 at 22:21
  • If one is going to use a dict, why not go with a defaultdict and get rid of an if/else block.
    – bixb0012
    Commented Jul 30, 2022 at 12:37
2

Are you looking for something like this. Add up all the values for different crops, report it out at the end.

I left out the format {} bits as I'm not familiar with that. Tried using brackets and index. May work as is or redo with {} and format if that is how you access values.

diversifiedSum = 0
pastureSum = 0

with arcpy.da.SearchCursor(fc,fields) as cursor:
  for row in cursor:
    if (row[0] = "Diversified"):
      diversifiedSum += row[1]
    if (row[0] = "Pasture"):
      pastureSum += row[1]

print("The crop Diversified had" + diversifiedSum + "acres gown in 2020")
print("The crop Pasture had" + pastureSum + "acres gown in 2020")
2
  • 4
    Rather than hard-coded crops values (such as "diversified..." and "pasture", when you do not know if there may be other values, it would be preferable to use a dictionary rather than two hard coded variables. Eg, cropDict[row[0]] += row[1], etc. Commented Jul 29, 2022 at 0:09
  • @SonofaBeach, How would you implement the cropDict[row[0]] += row[1] into the above code to return the other values. You are right there are multiple values for crop type, and I want to sum up the total amount acres for each crop type. Would I have to add in cropDict[row[0]] += row[1] as a conditional statement within the for loop?
    – Ryan Mags
    Commented Jul 29, 2022 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.