<?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: Export selected records to CSV using python (IDLE) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503453#M39549</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/165144" target="_blank"&gt;James Crandall&lt;/A&gt;‌ Excellent advice to use&amp;nbsp;pandas and numpy. If saved me a lot of headache trying to&amp;nbsp;make arcpy.TableToTable_conversion work in a certain out field order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By the way for others that don't know you can exclude the index added to the front of each row using index=False&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;"&gt;pdarr.to_csv(fileName,header=False, index=False)
&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;See also&amp;nbsp;&lt;A class="link-titled" href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html" title="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html" rel="nofollow noopener noreferrer" target="_blank"&gt;pandas.DataFrame.to_csv — pandas 0.23.3 documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:05:15 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-12-11T22:05:15Z</dc:date>
    <item>
      <title>Export selected records to CSV using python (IDLE)</title>
      <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503448#M39544</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all!&lt;/P&gt;&lt;P&gt;I have a polygon shapefile. It contains 200 records and a (long) field with reference numbers (type codes) 0, 1, 2 or 3.&lt;/P&gt;&lt;P&gt;I have a point shapefile with 25,000 records. The points fall inside the various polygons throughout the map.&lt;/P&gt;&lt;P&gt;I want to-&lt;/P&gt;&lt;P&gt;Select all points that intersect the polygons that have a type code of 0, and export this selection of points to a new CSV file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then repeat this process for ref number = 1, 2 and 3...&lt;/P&gt;&lt;P&gt;4 seperate scripts (one for each ref number) or 1 script doing it all at once: either would be fine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to do all this using the IDLE python GUI rather than going into ArcMap. So I want the script to point to shapefiles on my hard drive raher than layers in the Table Of Contents.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm still a noob at python so all I have on my mind so far is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Import the required python modules (I still don't quite know what this does)&lt;/P&gt;&lt;P&gt;??? import arcpy ???&lt;/P&gt;&lt;P&gt;??? from arcpy import env ???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# set the workspace environment&lt;/P&gt;&lt;P&gt;??? env.workspace = "C:\Data\" ???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Select all polygons where type code = 0 and export it to a temporary feature class&lt;/P&gt;&lt;P&gt;arcpy.Select_analysis('C:\Data\Polygon.shp', r'\\somernt\curo\users\faullt\ArcGIS\Default.gdb\Type_code_0_polygon', '"Type_code" = 0')&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# select all points that intersect the new 'type code = 0' feature class&lt;/P&gt;&lt;P&gt;arcpy.SelectLayerByLocation_management('C:\Data\Points.shp', 'INTERSECT', 'Scheme- unknown or no restriction', '#', 'NEW_SELECTION', 'NOT_INVERT')&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# export the new selection of points to a CSV file in C:\Data&lt;/P&gt;&lt;P&gt;??? ???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# display a completion message&lt;/P&gt;&lt;P&gt;print "CSV created"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The actual lines of python I have typed above are taken from dragging the results in the geoprocessing results pane into an ArcMap python window...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help would be much appreciated!&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:15:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503448#M39544</guid>
      <dc:creator>TheoFaull</dc:creator>
      <dc:date>2017-03-22T16:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: Export selected records to CSV using python (IDLE)</title>
      <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503449#M39545</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You say :&lt;/P&gt;&lt;P&gt;&lt;EM style="background-color: #ffffff;"&gt;The points fall inside the various polygons throughout the map&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;And&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;EM style="background-color: #ffffff;"&gt;Select all points that intersect the polygons that have a type code of 0, and export this selection of points to a new CSV &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;&lt;SPAN&gt;So you are not doing a join here? No info from the polygons is joined to the points?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;&lt;SPAN&gt;So why not just create 4 layers in your map using definition queries, then use the export to excel tool?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;&lt;SPAN&gt;If you know nothing about scripting or python, this will be the simpler way.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:55:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503449#M39545</guid>
      <dc:creator>NeilAyres</dc:creator>
      <dc:date>2017-03-22T16:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: Export selected records to CSV using python (IDLE)</title>
      <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503450#M39546</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No I don't want to join the datasets as I don't need any info from the polygon layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I just want a script that creates a CSV or Excel file of all points that fall within all the polygons where Type Code = 0 (and then 1, 2, 3)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Mar 2017 09:23:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503450#M39546</guid>
      <dc:creator>TheoFaull</dc:creator>
      <dc:date>2017-03-23T09:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Export selected records to CSV using python (IDLE)</title>
      <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503451#M39547</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm starting to believe I won't be able to do any of this without opening ArcMap, as the &lt;EM&gt;select by location&lt;/EM&gt; tool can only deal with layer inputs from the Table of Contents, not shapefile inputs from a C drive location.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Mar 2017 09:32:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503451#M39547</guid>
      <dc:creator>TheoFaull</dc:creator>
      <dc:date>2017-03-23T09:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: Export selected records to CSV using python (IDLE)</title>
      <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503452#M39548</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm typically using the numpy and pandas libraries and they have good export/saveas functionality.&amp;nbsp;Just convert your Feature Class to a numpy array and then save it to csv.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; numpy
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; pandas &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; pd
fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'H:\Documents\ArcGIS\Default.gdb\MyFeatureClass'&lt;/SPAN&gt;
nparr &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;FeatureClassToNumPyArray&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'Field1'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'Field2'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'Field3'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
pdarr &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; pd&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;DataFrame&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;nparr&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

pdarr&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;to_csv&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;r&lt;SPAN class="string token"&gt;'H:\nparr.csv'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:05:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503452#M39548</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T22:05:12Z</dc:date>
    </item>
    <item>
      <title>Re: Export selected records to CSV using python (IDLE)</title>
      <link>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503453#M39549</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/165144" target="_blank"&gt;James Crandall&lt;/A&gt;‌ Excellent advice to use&amp;nbsp;pandas and numpy. If saved me a lot of headache trying to&amp;nbsp;make arcpy.TableToTable_conversion work in a certain out field order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By the way for others that don't know you can exclude the index added to the front of each row using index=False&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;"&gt;pdarr.to_csv(fileName,header=False, index=False)
&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;See also&amp;nbsp;&lt;A class="link-titled" href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html" title="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html" rel="nofollow noopener noreferrer" target="_blank"&gt;pandas.DataFrame.to_csv — pandas 0.23.3 documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:05:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-selected-records-to-csv-using-python-idle/m-p/503453#M39549</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T22:05:15Z</dc:date>
    </item>
  </channel>
</rss>

