<jupyter_start><jupyter_text>Find cost of single family homes with given zip codes.<jupyter_code>import json
import requests
import pandas as pd
import csv
from config_zillow import api_key
import time
datafile1 = "../data/zipcodes.csv"
df1 = pd.read_csv(datafile1)
len(df1)<jupyter_output><empty_output><jupyter_text>Grab zipcode information.<jupyter_code>df1.head(10)<jupyter_output><empty_output><jupyter_text>Grab information about single family homes from Zillow.<jupyter_code>url = f"http://www.zillow.com/webservice/GetDemographics.htm?zws-id={api_key}&zip="

zip_code = df1["zip"]
new_url = []

for x in zip_code:
    n_url = f"{url}{x}"
    new_url.append(n_url)

text_container = []
for y in new_url:
    time.sleep(0.5)
    try:
        response = requests.get(y)
        response = response.text
        text_container.append(response)
        
    except:
        print ("Not available")

df = pd.DataFrame({
    "Text": text_container
})
df.to_csv("../data/dumpfile.csv")
zip_code = []
bbc = []
for x in text_container:
    try:
        ts = "</city><zip>"
        aa = x.split(ts)
        bb = aa[1].split("<")
        bbc.append(aa[1])
        zip_co = bb[0]
        zip_code.append(zip_co)

    except:
        continue

cost_home = []
for r in bbc:
    s = 'Median Single Family Home Value</name><values><nation><value type="USD">'    
    t = 'Median Single Family Home Value</name><values><zip><value type="USD">'
    try:
        aaa = r.split(t)
        bbb = aaa[1].split("<")
        home_co = bbb[0]
        cost_home.append(home_co)
    except:
        aaaa = r.split(s)
        bbbb = aaaa[1].split("<")
        home_co = bbbb[0]
        
        cost_home.append(home_co)
data = pd.DataFrame({
    "Zip Code": zip_code,
    "Cost of Single Family Home": cost_home
})

data["Zip Code"] = data["Zip Code"].astype("int")
data["Cost of Single Family Home"] = data["Cost of Single Family Home"].astype("float64")

data.to_csv("../data/Single_Family_Home_Cost.csv")
data.head(20)<jupyter_output><empty_output><jupyter_text>Grab information about single family homes from realtor.com csv file.<jupyter_code>datafile2 = "../data/realtor_info.csv"
df2 = pd.read_csv(datafile2)
df2["Median Listing Price"] = df2["Median Listing Price"].astype("float64")
df2["ZipCode"] = df2["ZipCode"].astype("int")


df2 = df2[["ZipCode", "Median Listing Price"]]
df2 = df2.rename(columns ={"ZipCode": "Zip Code"})
df2.head()<jupyter_output><empty_output><jupyter_text>Merge the two tables.  Find Average.<jupyter_code>data1 = pd.merge(data, df2, on = "Zip Code", how = "left")
data1.head()
data1["Median Listing Price"] = data1["Median Listing Price"].fillna(0)
data1.head(300)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15123570638140907, 'fraction_numerical': 0.016599040944300997, 'mean_word_length': 3.834224598930481, 'pattern_counts': {'":': 4, '<': 34, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '494012', 'license': 'MIT', 'n_tokens_mistral': 1024, 'n_tokens_neox': 986, 'n_words': 203, 'path': 'src/get_single_family_cost.ipynb', 'repo_name': 'knishina/clean_real_estate', 'size': '2711'}