<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculating the Mode from list of fields using field calculator in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310592#M71312</link>
    <description>&lt;P&gt;Hello Johannes,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your solution. I am now looking for a solution that is flexible to the field name input in the get_mode expression. What I have does not work but you should be able to see what I am trying to do. I am getting an error, unhashable type: 'list'. Is there a way to access the field names using a wildcard in the expression? Thank you in advance for your help.&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy import env
import statistics

arcpy.env.parallelProcessingFactor = "100%"
arcpy.env.overwriteOutput = "True"

env.workspace = "my path"

fieldName2 = "Most_Freq"
expression = "Get_mode(field_names)"
codeblock = """

def Get_mode(*args):
    return statistics.mode(args)"""

featureclasses = arcpy.ListFeatureClasses() 
for fc in featureclasses: 
    inTable = fc 
    print("Working on: {}".format(fc))
    field_names = [f.name for f in arcpy.ListFields(fc, "R*")]
    
    arcpy.management.AddField(inTable, fieldName2, "LONG")
    arcpy.management.CalculateField(inTable, fieldName, expression, "PYTHON3", codeblock)&lt;/LI-CODE&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Jul 2023 17:12:20 GMT</pubDate>
    <dc:creator>adam_gallaher</dc:creator>
    <dc:date>2023-07-21T17:12:20Z</dc:date>
    <item>
      <title>Calculating the Mode from list of fields using field calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310197#M71267</link>
      <description>&lt;P&gt;I am looking to calculate the mode for a new field from a list of fields using the field calculator. I cannot seem to find a method that works. I am open to pulling the data down and performing the calculations in python if field calculator is not capable. Thanks in advance for any help or suggestions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2023 19:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310197#M71267</guid>
      <dc:creator>adam_gallaher</dc:creator>
      <dc:date>2023-07-20T19:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the Mode from list of fields using field calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310227#M71273</link>
      <description>&lt;P&gt;Field Calculator, language = Python:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# ModeField = 
get_mode(!Field1!, !Field2!, !Field3!, !Field4!)

# code block
import statistics
def get_mode(*args):
    return statistics.mode(args)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 20 Jul 2023 20:17:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310227#M71273</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-07-20T20:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the Mode from list of fields using field calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310231#M71274</link>
      <description>&lt;P&gt;There are a number of ways you could do it, like creating an array of distinct values, then checking which show up the most frequently. It's a lot of code for something simple, but here it is:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fields_list = [
  'a',
  'b',
  'c'
]

var unique_values = []

for (var f in fields_list) {
  var the_val = $feature[fields_list[f]]

  // add unique values to array; skip if already in
  if (Includes(unique_values, the_val)) {
    continue
  } else {
    Push(unique_values, the_val)
  }
}

var modes = []

for (var u in unique_values) {
  var u_count = 0

  // if field has the unique value, increment it
  for (var f in fields_list) {
    if ($feature[fields_list[f]] == unique_values[u]) {
      u_count += 1
    }
  }

  /*
    if object in `modes` is smaller, replace it
    if equal, add to array
    otherwise skip
  */
  if (Count(modes) == 0 || First(modes)['count'] &amp;lt; u_count) {
    modes = [{'val': unique_values[u], 'count': u_count}]
  } else if (First(modes)['count'] == u_count) {
    Push(modes, {'val': unique_values[u], 'count': u_count})
  }
}

// our `modes` array should have 1 or more items in it now, which we can use to create an output
var out_lines = []

for (var m in modes) {
  Push(out_lines, modes[m]['val'])
}

return Concatenate(out_lines, '\n')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here it is run against a made up feature:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1689884533220.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75896iD06D5FB23A895717/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1689884533220.png" alt="jcarlson_0-1689884533220.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And if I make it so multiple values tie, I get all of the modes:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_1-1689884605145.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75897i8B0248EC9D504EA9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_1-1689884605145.png" alt="jcarlson_1-1689884605145.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_2-1689884632998.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75898i0483DAB2DD17CA9A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_2-1689884632998.png" alt="jcarlson_2-1689884632998.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2023 20:24:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310231#M71274</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-07-20T20:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the Mode from list of fields using field calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310592#M71312</link>
      <description>&lt;P&gt;Hello Johannes,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your solution. I am now looking for a solution that is flexible to the field name input in the get_mode expression. What I have does not work but you should be able to see what I am trying to do. I am getting an error, unhashable type: 'list'. Is there a way to access the field names using a wildcard in the expression? Thank you in advance for your help.&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy import env
import statistics

arcpy.env.parallelProcessingFactor = "100%"
arcpy.env.overwriteOutput = "True"

env.workspace = "my path"

fieldName2 = "Most_Freq"
expression = "Get_mode(field_names)"
codeblock = """

def Get_mode(*args):
    return statistics.mode(args)"""

featureclasses = arcpy.ListFeatureClasses() 
for fc in featureclasses: 
    inTable = fc 
    print("Working on: {}".format(fc))
    field_names = [f.name for f in arcpy.ListFields(fc, "R*")]
    
    arcpy.management.AddField(inTable, fieldName2, "LONG")
    arcpy.management.CalculateField(inTable, fieldName, expression, "PYTHON3", codeblock)&lt;/LI-CODE&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2023 17:12:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310592#M71312</guid>
      <dc:creator>adam_gallaher</dc:creator>
      <dc:date>2023-07-21T17:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the Mode from list of fields using field calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310714#M71317</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;unhashable type: 'list'.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;You get this error because&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;I used the &lt;STRONG&gt;*args&lt;/STRONG&gt; declaration in &lt;STRONG&gt;get_mode&lt;/STRONG&gt;. The asterisk is the unpacking operator. It is used here to allow any number of arguments and work with them as tuple inside the function.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;You are giving the fields as list, not as separate arguments.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;That means that the function calls &lt;STRONG&gt;statistics.mode(&amp;nbsp; ( [1, 2, 3], )&amp;nbsp; )&amp;nbsp;&lt;/STRONG&gt;, not &lt;STRONG&gt;statistics.mode(&amp;nbsp; [1, 2, 3]&amp;nbsp; )&lt;/STRONG&gt;, it inputs the list as one complete value to check.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;&lt;STRONG&gt;statistics.mode&lt;/STRONG&gt; uses a hashmap/dictionary internally to count the elements&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;So it tries to use the list as key for that hashmap. But lists are mutable, so they can't be hashed, thus the error.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;So the first step to fixing this is fixing the code block (just remove the asterisk):&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;codeblock = """
def Get_mode(args):
    return statistics.mode(args)
"""&lt;/LI-CODE&gt;&lt;P&gt;You could also skip the code block altogether and just call &lt;STRONG&gt;statistics.mode&lt;/STRONG&gt; directly in the expression field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next step is to fix your input list. Right now you're inputting string values. Assuming you have fields like "R1", "R2" and so on, you're doing this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;field_names = ["R1", "R2", "R3", "R4"]
statistics.mode(field_names)
# "R1"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to input the actual values. In the field calculator, you do this by enclosing the field name in exclamation points:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;field_names = ["!R1!", "!R2!", "!R3!", "!R4!"]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But that still won't work, because that will input this literal list and just return "!R1!" for each row.&lt;/P&gt;&lt;P&gt;Instead, you need to completely construct the whole expression instead of relying on an outside variable:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# get the field names
field_names = [f.name for f in arcpy.ListFields(inTable, "R*")]  # ['R1', 'R2', 'R3']
# surround them in exclamation marks
field_names = [f"!{n}!" for n in field_names]  # ['!R1!', '!R2!', '!R3!']
# concatenate
field_names = ", ".join(field_names)  # '!R1!, !R2!, !R3!'
# get the function call as string
expression = f"Get_mode([{field_names}])"  # 'Get_mode([!R1!, !R2!, !R3!])'

# alternatively: skip the code block and call statistics.mode directly:
expression = f"statistics.mode([{field_names}])"  # 'statistics.mode([!R1!, !R2!, !R3!])'


# and finally execute the FIeld Calculator
arcpy.management.CalculateField(inTable, fieldName2, expression, "PYTHON3", codeblock)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Personally, I don't like calling Calculate Field from a script. It's a tool for quick manual calculations, not for automating tasks. In your case, I'd just do it with an UpdateCursor:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import statistics

arcpy.env.workspace = "my path"
mode_field = "Most_Freq"
for fc in arcpy.ListFeatureClasses():
    arcpy.management.AddField(fc, mode_field, "LONG")
    value_fields = [f.name for f in arcpy.ListFields(fc, "R*")]
    with arcpy.da.UpdateCursor(fc, [mode_field] + value_fields) as cursor:
        for row in cursor:
            row[0] = statistics.mode(row[1:])
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2023 21:48:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310714#M71317</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-07-21T21:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the Mode from list of fields using field calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310719#M71318</link>
      <description>&lt;P&gt;Thank you for the explanation and solution. I'd agree, going the update cursor route is probably the more elegant solution. I found this to work as well. However, it might be better to update my code for future reference.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you again.&lt;/P&gt;&lt;P&gt;Adam&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy import env
import statistics
arcpy.env.parallelProcessingFactor = "100%"
arcpy.env.overwriteOutput = "True"
env.workspace = "my path"

fieldName2 = "Most_Freq"
expression = "Get_mode(', '.join(field_names))"
codeblock = """

def Get_mode(*args):
    return statistics.mode(args)"""

featureclasses = arcpy.ListFeatureClasses() 
for fc in featureclasses: 
    inTable = fc 
    print("Working on: {}".format(fc))
    field_names = ["!" + f.name + "!" for f in arcpy.ListFields(fc, "R*")]
   
    arcpy.management.AddField(inTable, fieldName2, "LONG")
    arcpy.management.CalculateField(inTable, fieldName, expression, "PYTHON3", codeblock)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 21 Jul 2023 21:53:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculating-the-mode-from-list-of-fields-using/m-p/1310719#M71318</guid>
      <dc:creator>adam_gallaher</dc:creator>
      <dc:date>2023-07-21T21:53:33Z</dc:date>
    </item>
  </channel>
</rss>

