[GIS] Calculate field based on most frequently occuring text/value in other fields in attribute table (ArcGIS)

arcgis-desktoparcpyfield-calculator

I was wondering if anyone knows if it is possible to calculate a field based on the most frequently occurring value in other fields.

What I have is 5 fields with crop information over 5 consecutive years. What I want is to create a new field with the most frequently occurring crop over that 5 year period.

So for example if there were fields for Crop 2008, Crop 2009, Crop 2010, Crop 2011 and Crop 2012 with respective crop types for the first polygon as Winter Wheat, Spring Barley, Winter Barley, Winter Wheat and Grass the function would return Winter Wheat because it appears twice.

I am aware there is a function called Counter in the Collections package that can do this for a list but I'm unsure how this would work within the arcpy field calculation tool?

Likewise there also needs to be a consideration for values that tie i.e. appear the same amount of times across the 5 fields.

I can't seem to find anything else from my searching about.

I'm using ArcGIS 10 and hoping to calculate fields in the python window.

Apologies for not providing any attempts at coding this so far, I really am stumped on this one!

Best Answer

you could try this in the field calculator, in case of tie it returns the smallest value.

PARSER :

PYTHON

CODEBLOCK:

def most_common(a,b,c,d,e):
    L = [a,b,c,d,e]
    newL = [x for x in L if x!=""]
    return max(set(newL), key=newL.count)

COMMAND:

most_common(!Crop2008!, !Crop2009!, !Crop2010!, !Crop2011!, !Crop2012!)
Related Question