<?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 How to export a FC into a csv reordering fields in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-export-a-fc-into-a-csv-reordering-fields/m-p/1413466#M70408</link>
    <description>&lt;P&gt;Good morning,&lt;/P&gt;&lt;P&gt;How can I export a FC to csv file, selecting some specific fields, and reorganizing them in a specific order needed by the application that is going to load this data?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Apr 2024 12:27:38 GMT</pubDate>
    <dc:creator>SanchezNuñez</dc:creator>
    <dc:date>2024-04-22T12:27:38Z</dc:date>
    <item>
      <title>How to export a FC into a csv reordering fields</title>
      <link>https://community.esri.com/t5/python-questions/how-to-export-a-fc-into-a-csv-reordering-fields/m-p/1413466#M70408</link>
      <description>&lt;P&gt;Good morning,&lt;/P&gt;&lt;P&gt;How can I export a FC to csv file, selecting some specific fields, and reorganizing them in a specific order needed by the application that is going to load this data?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 12:27:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-export-a-fc-into-a-csv-reordering-fields/m-p/1413466#M70408</guid>
      <dc:creator>SanchezNuñez</dc:creator>
      <dc:date>2024-04-22T12:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to export a FC into a csv reordering fields</title>
      <link>https://community.esri.com/t5/python-questions/how-to-export-a-fc-into-a-csv-reordering-fields/m-p/1413518#M70413</link>
      <description>&lt;P&gt;One option is to hard-code the desired field order into the script.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import pandas as pd

fc = r'https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_States_Generalized_Boundaries/FeatureServer/0'
fieldnames=[]
fields = arcpy.ListFields(fc)

# get list of field names from fc
for field in fields:
    print(field.name)
    fieldnames.append(field.name)

# convert FC to df
data = [row for row in arcpy.da.SearchCursor(fc, fieldnames)]
fc_dataframe = pd.DataFrame(data, columns=fieldnames)
print(fc_dataframe)

# remove specific fields
ListOfFieldsToRemove = ['OBJECTID','Shape__Area','Shape__Length','Shape']
fc_dataframe.drop(ListOfFieldsToRemove , axis=1, inplace = True)
print("printing dataframe...")
print(fc_dataframe)


# reorder fields
df =  fc_dataframe[['STATE_ABBR', 'STATE_FIPS', 'STATE_NAME', 'POPULATION', 'POP_SQMI', 'SQMI']]
df_reordered =  fc_dataframe[['STATE_ABBR', 'POP_SQMI', 'SQMI', 'STATE_FIPS', 'STATE_NAME','POPULATION']]                  
print("printing dataframe (with fields re-ordered)...")
print(df_reordered)

# export to csv
print("exporting to csv")
df.to_csv(r'C:\Temp\CSV_export.csv', index=False)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another option is to have a template .csv file and read the field order from that and then program your output to match the template.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 14:43:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-export-a-fc-into-a-csv-reordering-fields/m-p/1413518#M70413</guid>
      <dc:creator>Tom_Laue</dc:creator>
      <dc:date>2024-04-22T14:43:57Z</dc:date>
    </item>
  </channel>
</rss>

