Select to view content in your preferred language

Export pandas DataFrame to csv file using ArcGIS Pro script tool

2139
1
05-17-2023 01:19 PM
Luis
by
New Contributor II

I've created a script tool in ArcGIS Pro that asks the user to select a csv file, and then reads that csv file into a pandas DataFrame and performs some calculations. I'd like to be able to export a new csv file after the calculations are complete in the same folder as the input csv file.

For example, the user selects the input csv file from (C:\Users\Luis\GIS\input.csv). I'd like to export a new csv at the end of the script to (C:\Users\Luis\GIS\output.csv)

The parameter that reads in the user input is set up as follows:

input_csv = arcpy.GetParameterAsText(0)

The data type for this parameter is File is the Tool Properties in ArcGIS Pro.

Tags (3)
0 Kudos
1 Reply
Luke_Pinner
MVP Regular Contributor

Get the output csv filename as a parameter (output direction) and use df.to_csv to write it out.

input_csv = arcpy.GetParameterAsText(0)
output_csv = arcpy.GetParameterAsText(1)
df = pandas.read_csv(input_csv)
#do some calcs
df.to_csv(output_csv, index=False)