Select to view content in your preferred language

Creating a new tool in ArcGIS 10.8

483
2
12-11-2023 07:09 AM
AbdAllaAdlan
New Contributor

Hi everyone ..

I wrote a python code to sort an excel file containing parcel numbers and their areas, and the output is an excel file containing ranges of parcel numbers with same area in one record instead of each number in a record. I'm new to scripting in ArcGIS, I need someone to convert this code to a script tool in ArcGIS and to and to modify the code so that the input file is a Feature class table instead of an excel file but the output files remains as excel file.

 

Here is the code:

 

import pandas as pd

# Read the Excel file
input_file = 'original.xlsx' # Replace 'input.xlsx' with your Excel file name
output_file = 'test1.xlsx' # Replace 'output.xlsx' with your desired output file name

# Read the Excel file into a Pandas DataFrame
df = pd.read_excel(input_file)

# Sort the data by area and then by parcel number
# df = df.sort_values(by=['Area', 'Parcel Number'])

# Initialize variables to store output data
output_data = {'Parcel Range': [], 'Area': []}
current_area = None
current_range_start = None
previous_parcel = None

# Iterate through the data to create records as per the requirements
for index, row in df.iterrows():
parcel_num = row['Parcel Number']
area = row['Area']

if current_area != area or previous_parcel is None or parcel_num != previous_parcel + 1:
if current_area is not None:
if current_range_start == previous_parcel:
output_data['Parcel Range'].append(f"{current_range_start}")
else:
output_data['Parcel Range'].append(f"{current_range_start} - {previous_parcel}")
output_data['Area'].append(current_area)

current_area = area
current_range_start = parcel_num

previous_parcel = parcel_num

# Add the last range and area to the output
if current_area is not None:
output_data['Parcel Range'].append(f"{current_range_start} - {previous_parcel}")
output_data['Area'].append(current_area)

# Create a DataFrame from the output data
output_df = pd.DataFrame(output_data)

# Save the transformed data to a new Excel file
output_df.to_excel(output_file, index=False)

Tags (1)
2 Replies
ClayDonaldsonSWCA
Frequent Contributor

Setting up scripts as a tool is fairly simple. I would suggest reading the documentation here:

What is a script tool?—ArcMap | Documentation (arcgis.com)

0 Kudos
AbdAllaAdlan
New Contributor

Thank you. I tried but I faced some challenges, may be due to lack of experience.

0 Kudos