Pandas is the ideal tool for screening real estate data across hundreds of ZIP codes. This tutorial demonstrates a complete analysis workflow: data collection, cleaning, derived metrics, and ranking.
Building a Portfolio DataFrame
import requests, pandas as pd, time
def fetch_zip_data(zips, key):
rows = []
for z in zips:
for endpoint, params in [
("market-stats", {"zip_code": z}),
("rental-yield", {"zip_code": z, "bedrooms": 2}),
]:
r = requests.get(f"https://zipmarketdata.com/{endpoint}",
params=params,
headers={"x-rapidapi-proxy-secret": key}, timeout=10)
if r.ok:
rows.append({"zip": z, "endpoint": endpoint, **r.json()})
time.sleep(0.3)
return pd.DataFrame(rows)
Derived Metrics
df["price_to_rent"] = df["median_sale_price"] / (df["fair_market_rent"] * 12)
df["score"] = (
df["gross_yield_pct"].rank(pct=True) * 0.4 +
df["yoy_price_change"].rank(pct=True) * 0.3 +
(1 / df["median_days_on_market"]).rank(pct=True) * 0.3
)
top10 = df.nlargest(10, "score")