Select to view content in your preferred language

Python Toolbox: carry out multiple tasks in the source code def execute() function

98
1
Jump to solution
15 hours ago
JaredPilbeam2
MVP Alum

This code is in the def execute() function of a python toolbox.

  • A table is geocoded.
  • Then I want to remove spaces in two column names with the same out file parameter. 

The tool runs without error, and the file is saved to a directory. But, the spaces are not removed. I'm thinking this is because the arcpy.geocoding.GeocodeFile() function hasn't been returned yet. Can anyone clue me in?

def execute(self, parameters, messages):
        """The source code of the tool."""
        import arcpy
        import os
        import pandas as pd
        
        outfile = parameters[1].valueAsText
        outloc = os.path.dirname(outfile)
        outname = os.path.basename(outfile)
   
        arcpy.geocoding.GeocodeFile(
        in_table=parameters[0].valueAsText,
        locator="https://filepathto/GeocodeServer/Locators/Locator_AddPts",
        address_fields="'Address or Place' ADDRESS VISIBLE NONE;Address2 <None> VISIBLE NONE;Address3 <None> VISIBLE NONE;Neighborhood <None> VISIBLE NONE;City CITY VISIBLE NONE;County COUNTY VISIBLE NONE;State STATE VISIBLE NONE;ZIP ZIPCODE VISIBLE NONE;ZIP4 <None> VISIBLE NONE;Country <None> VISIBLE NONE",
        output_type="CSV",
        output_location=outloc,
        output_name=outname,
        country=None,
        location_type="ROUTING_LOCATION",
        category=None,
        output_fields="LOCATION_ONLY"
        )
        
        # Pandas section ------
        df = pd.read_csv(parameters[1].valueAsText)

        #replace white space in column name with no white space
        df = pd.DataFrame(columns=['Shape X','Shape Y'])
        df.columns = [col.replace(' ','') if isinstance(col, str) else col for col in df.columns]
        df.to_csv(os.path.join(parameters[1].valueAsText))
        
        return

 

I tried putting the pandas section in this section of the python toolbox, but it produces an error and I'm not even sure what goes here.

   def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""

        return

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
Robert_LeClair
Esri Esteemed Contributor

This line is the problem:

df = DataFrame(columns=['Shape X', 'Shape Y'])

It replaces df (the CSV you read) with a new a new DataFrame that only has those two columns.

Try these changes: 

# Pandas section ------

df = pd.read_csv(parameters[1].valueAsText)

# remove spaces from ALL column names

df.columns = df.columns.str.replace(" ", "", regex=False)

# write back to the same output CSV

df.to_csv(parameters[1].valueAsText, index=False)

View solution in original post

0 Kudos
1 Reply
Robert_LeClair
Esri Esteemed Contributor

This line is the problem:

df = DataFrame(columns=['Shape X', 'Shape Y'])

It replaces df (the CSV you read) with a new a new DataFrame that only has those two columns.

Try these changes: 

# Pandas section ------

df = pd.read_csv(parameters[1].valueAsText)

# remove spaces from ALL column names

df.columns = df.columns.str.replace(" ", "", regex=False)

# write back to the same output CSV

df.to_csv(parameters[1].valueAsText, index=False)

0 Kudos