Good morning,
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?
Thanks
One option is to hard-code the desired field order into the script.
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)
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.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.