<?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: Using count and group by in makeQueryTable_Management in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25560#M1924</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks. This is what I put and I am getting a syntax error&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def count_duplicates("I/live_data.dbf",'id'):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Add the count field&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data_layer = arcpy.MakeFeatureLayer_management("I:/live_data.dbf", 'data')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(data_layer, 'COUNT', 'LONG')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(data_layer)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del data_layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; #Do the counting:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_list = []&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor("I/live_data.dbf", ['id']) as search_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in search_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_list.append(row[0])&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; counts = Counter(value_list)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del value_list&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor("I/live_data.dbf", ['id', 'COUNT']) as update_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in udpate_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = counts[row[0]]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; update_rows.updateRow(row)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 29 Oct 2013 15:08:58 GMT</pubDate>
    <dc:creator>DaveTaylor</dc:creator>
    <dc:date>2013-10-29T15:08:58Z</dc:date>
    <item>
      <title>Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25558#M1922</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I know you can't use the count and group by in the where clause of the MakeQueryTable_Management function but is there a way using python to count the rows that are the same once done?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have the function working so that it exports to a .dbf file but instead of having them grouped by ID's it just has them all listed separately. For instance:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ID&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1234&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2345&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2346&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1234&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2345&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I want is this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COUNT&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1234&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2345&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2346&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is there something I can use in python to either group all the same id's together and add a new column to my dbf or is there something with ESRI I can use?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My end result is I want to join this to a shapefile I already have and color code them by counts and have it update on the fly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 14:24:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25558#M1922</guid>
      <dc:creator>DaveTaylor</dc:creator>
      <dc:date>2013-10-29T14:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25559#M1923</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Something along the lines of the following code should work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
from collections import Counter

def count_duplicates(path_to_data, field_to_count):
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Add the count field
&amp;nbsp;&amp;nbsp;&amp;nbsp; data_layer = arcpy.MakeFeatureLayer_management(path_to_data, 'data')
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(data_layer, 'COUNT', 'LONG')
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(data_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del data_layer

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Do the counting:
&amp;nbsp;&amp;nbsp;&amp;nbsp; value_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(path_to_data, [field_to_count]) as search_rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in search_rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_list.append(row[0])

&amp;nbsp;&amp;nbsp;&amp;nbsp; counts = Counter(value_list)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del value_list
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(path_to_data, [field_to_count, 'COUNT']) as update_rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in udpate_rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = counts[row[0]]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; update_rows.updateRow(row)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:02:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25559#M1923</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-10T21:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25560#M1924</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks. This is what I put and I am getting a syntax error&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def count_duplicates("I/live_data.dbf",'id'):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Add the count field&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data_layer = arcpy.MakeFeatureLayer_management("I:/live_data.dbf", 'data')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(data_layer, 'COUNT', 'LONG')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(data_layer)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del data_layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; #Do the counting:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_list = []&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor("I/live_data.dbf", ['id']) as search_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in search_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_list.append(row[0])&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; counts = Counter(value_list)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del value_list&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor("I/live_data.dbf", ['id', 'COUNT']) as update_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in udpate_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = counts[row[0]]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; update_rows.updateRow(row)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 15:08:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25560#M1924</guid>
      <dc:creator>DaveTaylor</dc:creator>
      <dc:date>2013-10-29T15:08:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25561#M1925</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Using your example, to execute the function keep what I originally posted and add the following at the end:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
count_duplicates("I/live_data.dbf",'id')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since the code is defined as a function, you need to call it at the end rather than replacing the parameters. Also to be sure you have the correct file path you can navigate to it in Windows Explorer and hold shift, then right click on the file. This enables the option "Copy as text".&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:02:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25561#M1925</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-10T21:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25562#M1926</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You could also accomplish the same thing by using&lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00080000001z000000"&gt; Summary Statistics&lt;/A&gt;&lt;SPAN&gt;, and then joining the resulting DBF table back to your source dataset. This could be easily done in model builder also.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 16:22:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25562#M1926</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-10-29T16:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25563#M1927</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Unless I'm missing the point, calling the Summary Statistics tool will do this in one line of code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.Statistics_analysis("Export_Output","in_memory/x",[["ID","COUNT"]],"ID")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This creates a table called X in the in_memory workspace and returns a count on a field called ID.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 16:28:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25563#M1927</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2013-10-29T16:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25564#M1928</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It would. I overthought the issue initially, although cursors are usually faster with large data sets. The join back is necessary so that the features can be symbolized by the count.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 16:37:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25564#M1928</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-10-29T16:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25565#M1929</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Unless I'm missing the point, calling the Summary Statistics tool will do this in one line of code?&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.Statistics_analysis("Export_Output","in_memory/x",[["ID","COUNT"]],"ID")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;This creates a table called X in the in_memory workspace and returns a count on a field called ID.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;How do I get this out of memory and saved as an actual .dbf file?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 16:38:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25565#M1929</guid>
      <dc:creator>DaveTaylor</dc:creator>
      <dc:date>2013-10-29T16:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using count and group by in makeQueryTable_Management</title>
      <link>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25566#M1930</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;nevermind I think I got it! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks that appears to work perfectly!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Oct 2013 16:41:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-count-and-group-by-in-makequerytable/m-p/25566#M1930</guid>
      <dc:creator>DaveTaylor</dc:creator>
      <dc:date>2013-10-29T16:41:33Z</dc:date>
    </item>
  </channel>
</rss>

