ready_table_path = 'Table with average monthly savings(within 0.5 Mile) and tree count(within 0.5 Mile).csv'
search_radius_in_miles = 0.5
import pandas as pd
import numpy as np
ready_table = pd.read_csv(ready_table_path)
ready_table = ready_table[ready_table['Industry'] == 'Commercial']
ready_table = ready_table[ready_table['Program'] == 'ICAP']
ready_table[:3]
# Make kdtree
from pysal.cg.kdtree import KDTree
from pysal.cg import RADIUS_EARTH_MILES
sav_xys = ready_table[['Longitude', 'Latitude']].values
bin_sav = KDTree(sav_xys, distance_metric='Arc', radius=RADIUS_EARTH_MILES)
sav_count = len(sav_xys)
sav_xys
def get_sav_average(r):
xy = r['Longitude'], r['Latitude']
distances, indices = bin_sav.query(
xy,
k=len(bin_sav.data),
distance_upper_bound=search_radius_in_miles)
indices = indices[~np.isnan(indices)]
indices = [int(x) for x in indices]
selected_sav_table = ready_table.loc[ready_table.index[indices]]
#selected_sav_table = ready_table[ready_table['Industry'] == 'Commercial']
#selected_sav_table = ready_table[ready_table['Program'] == 'ICAP']
return selected_sav_table['Periodic Savings'].mean()
ready_table['Periodic Savings for your comparables within 0.5 Mile'] = ready_table.apply(get_sav_average, axis=1)
ready_table[:3]