<?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: Begin da.SearchCursor at a specific row in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009503#M59272</link>
    <description>&lt;P&gt;Something along the lines of:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy import da
from itertools import dropwhile

fc = # path to feature class
flds =  # list of fields
orderby = # sql order by statement for sorting
rand_row = # row number to start cursor

with da.SearchCursor(fc, flds, sql_clause=(None,orderby)) as cur:
    for row in dropwhile(lambda x: x[0] &amp;lt; rand_row, enumerate(cur, 1)):
        print(row)
    
    cur.reset()
    for row in cur:
        print(row)    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 14 Dec 2020 23:33:11 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2020-12-14T23:33:11Z</dc:date>
    <item>
      <title>Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009457#M59268</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm trying to start reading through a da.SearchCursor at a specific row (this row is randomly selected), but I can't figure out how to specify that in a search cursor.&amp;nbsp; I want to do something similar to a slice in a list (&lt;A href="https://stackoverflow.com/questions/509211/understanding-slice-notation" target="_blank"&gt;https://stackoverflow.com/questions/509211/understanding-slice-notation)&lt;/A&gt;&amp;nbsp;but this syntax doesn't seem to work in a searchcursor.&amp;nbsp; &amp;nbsp;Basically, I want to start running through my searchcursor starting at a random row.&amp;nbsp; This table has to be sorted though, so I can't just randomize the the rows within the table, but I need to first sort the table based on attributes, then create a random number that is between 0 and the number of records in that table (I can figure that much out), but then I need to specify in the searchcursor to start at that random number.&amp;nbsp; So for instance, say I have 20 records in my table, and I create a random number that is 3, I want to start my search cursor at row 3 in my table (and skip the first 2 rows).&amp;nbsp; This is where I am stuck.&amp;nbsp; Does anyone have any ideas how to do this?&lt;/P&gt;&lt;P&gt;In addition, after I figure that out, I'll want to re-loop around and start at the beginning of the search cursor (the actual first row), if anyone has any suggestions on that.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 20:55:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009457#M59268</guid>
      <dc:creator>BrookeHodge</dc:creator>
      <dc:date>2020-12-14T20:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009465#M59269</link>
      <description>&lt;P&gt;Interesting problem. Here's my first thought...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import random

row_count = int(arcpy.GetCount_management(fc).getOutput(0))
random_rownum = random.randint(1, row_count)

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for rownum, row in enumerate(cursor, start=1):
        if rownum &amp;gt;= random_rownum:
            # do something
    cursor.reset()
    for rownum, row in enumerate(cursor, start=1):
        if rownum &amp;lt; random_rownum:
            # do something
        else:
            break&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This isn't ultra efficient so if performance matters and you have many (millions) of rows, this might be a little slower because it's looping over all the rows twice.&lt;/P&gt;&lt;P&gt;EDIT:&lt;/P&gt;&lt;P&gt;I added a break at the end after finishing the first portion of records so it reduces the extra iteration by half.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 21:18:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009465#M59269</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2020-12-14T21:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009467#M59270</link>
      <description>&lt;P&gt;Have you got any code?&lt;/P&gt;&lt;P&gt;I'm a newbie, but my first thought is that you would generate and store your random cursor row number in a variable.&lt;/P&gt;&lt;P&gt;Now I thought that SearchCursor took a parameter that said which rows it needed to start with.&amp;nbsp; The SC goes until the end, at which point, I believe you would have to re-create the cursor object in order to start at the beginning of the table and run to the row you specified or call the cursor's reset() method.&amp;nbsp; I do believe this puts it back at the first row, but I'm guessing you would have to build in logic if your randomly generated row ended up being the first row?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another thought where perhaps you need to figure out your row, then extract all the rows you want to iterate through into their own list, and then run the search cursor from there?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 21:17:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009467#M59270</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2020-12-14T21:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009472#M59271</link>
      <description>&lt;P&gt;What are you doing with the data?&lt;/P&gt;&lt;P&gt;What you describe sounds like a python, numpy job&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/arcpy/data-access/tabletonumpyarray.htm" target="_blank"&gt;TableToNumPyArray—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;They are designed to work with tabular data and excel at slicing, dicing, sorting and statistical summary&lt;/P&gt;&lt;LI-CODE lang="python"&gt;d = tbl_data(fc3)   # ----- just some data
names = d.dtype.names  # ---- the column names
points = d['PNT_COUNT']   # ---- a column
srted = np.sort(d, axis=0, order='PNT_COUNT')  # ---- sort on that column

names
('OBJECTID', 'ids', 'CENTROID_X', 'CENTROID_Y', 'INSIDE_X',
 'INSIDE_Y', 'PART_COUNT', 'PNT_COUNT', 'Sort_')

d
array([(1, 11,  ...,   1.000,  11.000,    3),
       (2, 12,  ...,   1.000,   8.000,    2),
       (3, 13,  ...,   1.000,   6.000,    1),
       (4, 14,  ...,   1.000,   5.000,    0),
       (5, 15,  ...,   1.000,  10.000, -999),
       (6, 16,  ...,   1.000,  10.000, -999),
       (9, 19,  ...,   1.000,   5.000, -999)],
      dtype=[('OBJECTID', '&amp;lt;i4'), ('ids', '&amp;lt;i4'), ...,
             ('PART_COUNT', '&amp;lt;f8'), ('PNT_COUNT', '&amp;lt;f8'), ('Sort_', '&amp;lt;i4')])

points
array([ 11.000,   8.000,   6.000,   5.000,  10.000,  10.000,   5.000])

srted
array([(4, 14,  ...,   1.000,   5.000,    0),
       (9, 19,  ...,   1.000,   5.000, -999),
       (3, 13,  ...,   1.000,   6.000,    1),
       (2, 12,  ...,   1.000,   8.000,    2),
       (5, 15,  ...,   1.000,  10.000, -999),
       (6, 16,  ...,   1.000,  10.000, -999),
       (1, 11,  ...,   1.000,  11.000,    3)],
      dtype=[('OBJECTID', '&amp;lt;i4'), ('ids', '&amp;lt;i4'), ...,
             ('PART_COUNT', '&amp;lt;f8'), ('PNT_COUNT', '&amp;lt;f8'), ('Sort_', '&amp;lt;i4')])

d[:3]  # ---- a slice of the first 3 rows
array([(1, 11,  ...,   1.000,  11.000, 3),
       (2, 12,  ...,   1.000,   8.000, 2),
       (3, 13,  ...,   1.000,   6.000, 1)],
      dtype=[('OBJECTID', '&amp;lt;i4'), ('ids', '&amp;lt;i4'),...,
             ('PART_COUNT', '&amp;lt;f8'), ('PNT_COUNT', '&amp;lt;f8'), ('Sort_', '&amp;lt;i4')])

sub = d[['OBJECTID', 'PNT_COUNT', 'Sort_']]  # ---- a slice of 3 columns
sub
array([(1,  11.000,    3), (2,   8.000,    2), (3,   6.000,    1),
       (4,   5.000,    0), (5,  10.000, -999),
       (6,  10.000, -999), (9,   5.000, -999)],
      ....&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 21:22:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009472#M59271</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2020-12-14T21:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009503#M59272</link>
      <description>&lt;P&gt;Something along the lines of:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy import da
from itertools import dropwhile

fc = # path to feature class
flds =  # list of fields
orderby = # sql order by statement for sorting
rand_row = # row number to start cursor

with da.SearchCursor(fc, flds, sql_clause=(None,orderby)) as cur:
    for row in dropwhile(lambda x: x[0] &amp;lt; rand_row, enumerate(cur, 1)):
        print(row)
    
    cur.reset()
    for row in cur:
        print(row)    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 23:33:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009503#M59272</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-12-14T23:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009619#M59274</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I've never heard of&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;itertools.dropwhile()&lt;/FONT&gt;&lt;SPAN&gt;. Just another clever tool I'll have to remember for the future!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 00:52:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009619#M59274</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2020-12-15T00:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009777#M59276</link>
      <description>&lt;P&gt;Thanks for your reply and your suggestion!&amp;nbsp; This seemed to work just as I wanted it to!&amp;nbsp; After the reset, I just continued with a standard 'for row in cur:' since I just wanted it to start from the beginning, I didn't need to test if it was less than the random&amp;nbsp; number, but this solution seemed to work just as I wanted.&amp;nbsp; Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 16:33:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009777#M59276</guid>
      <dc:creator>BrookeHodge</dc:creator>
      <dc:date>2020-12-15T16:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Begin da.SearchCursor at a specific row</title>
      <link>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009780#M59277</link>
      <description>&lt;P&gt;Thank for your reply and suggestion!&amp;nbsp; I did try this and I was getting an index error thrown.&amp;nbsp; My guess is this solution would work if I better understood the dropwhile() and then probably could have trouble shoot my error to get it to work.&amp;nbsp; I ended up trying BlakeTerhune suggestion which worked.&amp;nbsp; But I had never heard of itertools before and can see great utility in them for the future, so I'm glad you made the suggestion.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 16:38:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/begin-da-searchcursor-at-a-specific-row/m-p/1009780#M59277</guid>
      <dc:creator>BrookeHodge</dc:creator>
      <dc:date>2020-12-15T16:38:05Z</dc:date>
    </item>
  </channel>
</rss>

