Solution and implementation for Q10 from Data Modeling & Visualization (dmv).
Problem Statement: Data Wrangling on Real Estate Market
import re
import pandas as pd
import matplotlib.pyplot as plt
homes = pd.read_csv("RealEstate_Prices.csv")
amenities = pd.read_csv("RealEstate_Neighborhoods.csv")
print("Raw shape:", homes.shape)
print(homes.head().to_string(index=False))
print("Missing values:\n", homes.isna().sum().to_string())
# Clean column names and types; fill the missing area with the median.
homes.columns = [re.sub(r"[^a-z0-9]+", "_", name.lower()).strip("_") for name in homes.columns]
homes["sale_date"] = pd.to_datetime(homes["sale_date"], errors="coerce")
homes["neighborhood"] = homes["neighborhood"].astype(str).str.strip().str.title()
homes["area_sqft"] = homes["area_sqft"].fillna(homes["area_sqft"].median())
homes = homes.dropna(subset=["sale_date", "sale_price"])
print("Clean columns:", homes.columns.tolist())
# Merge neighborhood amenities and select a recent subset.
homes = homes.merge(amenities, left_on="neighborhood", right_on="Neighborhood", how="left", validate="many_to_one")
homes = homes.drop(columns="Neighborhood")
recent = homes.loc[homes["sale_date"] >= "2024-01-01"].copy()
print("Recent properties:", len(recent))
print(recent[["neighborhood", "NearbyAmenities"]].head().to_string(index=False))
# Cap unusually high prices and derive price per square foot.
q1, q3 = recent["sale_price"].quantile([0.25, 0.75])
upper = q3 + 1.5 * (q3 - q1)
print("Prices above IQR upper bound:", int((recent["sale_price"] > upper).sum()))
recent["sale_price_capped"] = recent["sale_price"].clip(upper=upper)
recent["price_per_sqft"] = recent["sale_price_capped"] / recent["area_sqft"]
print(recent.groupby("neighborhood")["sale_price_capped"].agg(["count", "mean", "median"]).round(2).to_string())
# Encode categorical columns for later modeling and compare neighborhoods.
model_features = pd.get_dummies(recent[["neighborhood", "property_type", "area_sqft", "bedrooms", "year_built", "NearbyAmenities"]], drop_first=True, dtype=int)
print("Encoded feature columns:", model_features.columns.tolist())
recent.groupby("neighborhood")["sale_price_capped"].mean().sort_values().plot.barh(title="Average sale price by neighborhood")
plt.xlabel("Sale price")
plt.tight_layout()
plt.show()