pythonpandasdataframearcgisarcpy

Export pandas DataFrame to csv file using ArcGIS Pro script tool


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.


Solution

  • If all you want is to get the folder the input file is in and the input_csv parameter is an absolute path you can use the following

    input_csv = arcpy.GetParameterAsText(0)
    output_csv = input_csv.split('\\')[:-1]
    output_csv = '\\'.join(output_csv + ['output.csv'])
    

    Then save your DataFrame using the new filepath

    df.to_csv(output_csv)