Files
Territory-Analysis---Mapper/process_territories.py

26 lines
1.0 KiB
Python

import pandas as pd
from datetime import datetime
def process_data(addresses_file, boundaries_file):
"""
Reads address and boundary CSVs, merges them, and returns a consolidated DataFrame.
"""
try:
# Read the addresses and count occurrences of each TerritoryID
address_counts = pd.read_csv(addresses_file).groupby('TerritoryID').size().reset_index(name='Address Count')
# Read the boundaries file
boundaries_df = pd.read_csv(boundaries_file)
# Merge the address counts with the boundaries data
merged_df = pd.merge(boundaries_df, address_counts, on='TerritoryID', how='left')
# Fill missing address counts with 0 and ensure the column is integer type
merged_df['Address Count'] = merged_df['Address Count'].fillna(0).astype(int)
return merged_df
except FileNotFoundError as e:
raise FileNotFoundError(f"Error during data processing: {e}")
except Exception as e:
raise Exception(f"An unexpected error occurred during data processing: {e}")