<?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 Is there a way to count the records in a feature layer? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352936#M27692</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I need to display several layers from the same feature class using definition queries for different systems and using the same template for different projects. While I have created a script that can automatically produce these layers in an MXD, the definition query will often reduce number of records to zero for certain systems, eliminating the need for it as a layer. I would like to use an IF statement with arcpy.mapping.RemoveLayer to automatically remove those layers that do not return results, but not sure which functions may be able to do this. I have looked into using SearchCursor but do not otherwise know how to identify the number of records in the attribute database for a particular layer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 26 Mar 2012 20:03:29 GMT</pubDate>
    <dc:creator>JohnPersley</dc:creator>
    <dc:date>2012-03-26T20:03:29Z</dc:date>
    <item>
      <title>Is there a way to count the records in a feature layer?</title>
      <link>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352936#M27692</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I need to display several layers from the same feature class using definition queries for different systems and using the same template for different projects. While I have created a script that can automatically produce these layers in an MXD, the definition query will often reduce number of records to zero for certain systems, eliminating the need for it as a layer. I would like to use an IF statement with arcpy.mapping.RemoveLayer to automatically remove those layers that do not return results, but not sure which functions may be able to do this. I have looked into using SearchCursor but do not otherwise know how to identify the number of records in the attribute database for a particular layer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Mar 2012 20:03:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352936#M27692</guid>
      <dc:creator>JohnPersley</dc:creator>
      <dc:date>2012-03-26T20:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to count the records in a feature layer?</title>
      <link>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352937#M27693</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This thread covers a similar problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/49172-how-to-check-if-cursor-object-is-empty?highlight=empty+layer+cursor"&gt;http://forums.arcgis.com/threads/49172-how-to-check-if-cursor-object-is-empty?highlight=empty+layer+cursor&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Mar 2012 20:22:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352937#M27693</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-26T20:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to count the records in a feature layer?</title>
      <link>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352938#M27694</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Would arcpy.GetCount_management() help?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Mar 2012 22:01:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352938#M27694</guid>
      <dc:creator>StephenBarrow</dc:creator>
      <dc:date>2012-03-26T22:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to count the records in a feature layer?</title>
      <link>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352939#M27695</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi John,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Matthew and Stephen suggestions are right on track.&amp;nbsp; Either using the Search Cursor, or the Get Count will be identical in performance.&amp;nbsp; Example using Search Cursor:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument(r"C:\Projects\\Municipality.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(lyr, ["OID@"], "OBJECTID &amp;gt; 0") as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(row[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = len(list)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if count == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del mxd&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example using Get Count:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument(r"C:\Projects\Municipality.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = int(result.getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if count == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....
del mxd&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:33:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352939#M27695</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T16:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to count the records in a feature layer?</title>
      <link>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352940#M27696</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Both techniques will/do work, but in my experience the performance is not identical between the two.&amp;nbsp; For small to medium datasets (~100,000 records or less), the performance can be quite similar with the arcpy.da.SearchCursor and arcpy.GetCount_management.&amp;nbsp; For larger datasets, I have seen GetCount outperform cursors by 3 to 10 times.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3016"&gt;Jason Scheirer&lt;/A&gt;​ speaks to why using a GP tool is faster than a Python cursor with larger datasets:&amp;nbsp; &lt;A href="https://community.esri.com/message/372116"&gt;Re: How to determine number of records using arcpy.da.SearchCursor&lt;/A&gt;​.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Jul 2015 02:16:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/is-there-a-way-to-count-the-records-in-a-feature/m-p/352940#M27696</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-07-01T02:16:09Z</dc:date>
    </item>
  </channel>
</rss>

