In [33]:
from zoopla import Zoopla
import pandas as pd
import geopy.distance

print('modules imported')

zoopla = Zoopla(api_key='api_key')
    
search = zoopla.property_listings({
    'maximum_beds': 2,
    'page_size': 100,
    'listing_status': 'sale',
    'area': 'London'
})

school = pd.read_csv('ldn.csv',encoding ='latin1')
schools = school.where(school['PhaseOfEducation (name)'] != 'Not applicable')

print('school data loaded')

allot = pd.read_csv('allot.csv',encoding ='latin1')

print('allot data loaded')

raw_data = pd.DataFrame([])

for result in search.listing:
    d = {'img': [result.image_url], 'price': [result.price], 'url': [result.details_url], 'desc': [result.description], 'lat': [result.latitude], 'long': [result.longitude], 'status': [result.listing_status],'town': [result.post_town], 'beds': [result.num_bedrooms], 'baths': [result.num_bathrooms], 'street': [result.street_name]}
    df = pd.DataFrame(data=d)
    raw_data = raw_data.append(df ,ignore_index=True,sort=False)

print('start loop through properties')
#Loop through property data
for index, row in raw_data.iterrows():  
    start = (row['lat'], row['long'])
    
    #calculate how far from the property the schools are
    print('start loop through schools')
    for indexx, row in schools.iterrows():
        schoolloc = (row['Latitude'], row['Longitude'])
        try:
            schools.loc[indexx,'distance'] = geopy.distance.geodesic(start, schoolloc).km
        except:
            schools.loc[indexx,'distance'] = 9999
            
    #calculate how far from the property the allotments are        
    for indexxx, row in allot.iterrows():
        allotloc = (row['Latitude'], row['Longitude'])
        try:
            allot.loc[indexxx,'distance'] = geopy.distance.geodesic(start, allotloc).km
        except:
            allot.loc[indexxx,'distance'] = 9999

    #find the closest primary & save to dataframe
    primary = schools.where(schools['PhaseOfEducation (name)'] == 'Primary')
    Nearest_Primary = primary[primary['distance'] == min(primary['distance'])]
    Primary_Name = Nearest_Primary.iloc[0]['EstablishmentName']
    Primary_Distance = Nearest_Primary.iloc[0]['distance']
    print('property found')

    #find closest secondary school
    secondary = schools.where(schools['PhaseOfEducation (name)'] == 'Secondary')
    secondary = secondary.dropna()
    Nearest_Secondary = secondary[secondary['distance'] == min(secondary['distance'])]
    Secondary_Name = Nearest_Secondary.iloc[0]['EstablishmentName']
    Secondary_Distance = Nearest_Secondary.iloc[0]['distance']

    #find closest nursery
    nursery = schools.where(schools['PhaseOfEducation (name)'] == 'Nursery')
    nursery = nursery.dropna()
    Nearest_Nursery = nursery[nursery['distance'] == min(nursery['distance'])]
    nursery_name = Nearest_Nursery.iloc[0]['EstablishmentName']
    nursery_Distance = Nearest_Nursery.iloc[0]['distance']
    
    #find the closest allotment & save to dataframe
    Nearest_allot = allot[allot['distance'] == min(allot['distance'])]
    Allot_Name = Nearest_allot.iloc[0]['Name']
    Allot_Distance = Nearest_allot.iloc[0]['distance']
    print('property found')    
    
    raw_data.loc[[index], 'Primary School Name'] = Primary_Name
    raw_data.loc[[index], 'Primary Distance'] = Primary_Distance
    raw_data.loc[[index], 'Secondary School Name'] = Secondary_Name
    raw_data.loc[[index], 'Secondary Distance'] = Secondary_Distance
    raw_data.loc[[index], 'Nursery School Name'] = nursery_name
    raw_data.loc[[index], 'Nursery Distance'] = nursery_Distance
    raw_data.loc[[index], 'Allotment Name'] = Allot_Name
    raw_data.loc[[index], 'Allotment Distance'] = Allot_Distance
    print('updated property')
modules imported
school data loaded
allot data loaded
start loop through properties
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
start loop through schools
property found
property found
updated property
In [ ]: