|
|
|
|
|
|
|
|
|
|
|
|
|
import pandas as pd |
|
import os |
|
|
|
from helpers import ( |
|
get_combined_df, |
|
save_final_df_as_jsonl, |
|
handle_slug_column_mappings, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
DATA_DIR = "../data" |
|
PROCESSED_DIR = "../processed/" |
|
FACET_DIR = "rentals/" |
|
FULL_DATA_DIR_PATH = os.path.join(DATA_DIR, FACET_DIR) |
|
FULL_PROCESSED_DIR_PATH = os.path.join(PROCESSED_DIR, FACET_DIR) |
|
|
|
|
|
|
|
|
|
|
|
data_frames = [] |
|
|
|
slug_column_mappings = {"": "Rent"} |
|
|
|
for filename in os.listdir(FULL_DATA_DIR_PATH): |
|
if filename.endswith(".csv"): |
|
|
|
cur_df = pd.read_csv(os.path.join(FULL_DATA_DIR_PATH, filename)) |
|
exclude_columns = [ |
|
"RegionID", |
|
"SizeRank", |
|
"RegionName", |
|
"RegionType", |
|
"StateName", |
|
"Home Type", |
|
] |
|
|
|
if "_sfrcondomfr_" in filename: |
|
cur_df["Home Type"] = "all homes plus multifamily" |
|
|
|
cur_df["RegionName"] = cur_df["RegionName"].astype(str) |
|
if "City" in filename: |
|
exclude_columns = [ |
|
"RegionID", |
|
"SizeRank", |
|
"RegionName", |
|
"RegionType", |
|
"StateName", |
|
"Home Type", |
|
|
|
"State", |
|
"Metro", |
|
"CountyName", |
|
] |
|
elif "Zip" in filename: |
|
exclude_columns = [ |
|
"RegionID", |
|
"SizeRank", |
|
"RegionName", |
|
"RegionType", |
|
"StateName", |
|
"Home Type", |
|
|
|
"State", |
|
"City", |
|
"Metro", |
|
"CountyName", |
|
] |
|
elif "County" in filename: |
|
exclude_columns = [ |
|
"RegionID", |
|
"SizeRank", |
|
"RegionName", |
|
"RegionType", |
|
"StateName", |
|
"Home Type", |
|
|
|
"State", |
|
"Metro", |
|
"StateCodeFIPS", |
|
"MunicipalCodeFIPS", |
|
] |
|
|
|
elif "_sfr_" in filename: |
|
cur_df["Home Type"] = "SFR" |
|
elif "_mfr_" in filename: |
|
cur_df["Home Type"] = "multifamily" |
|
|
|
data_frames = handle_slug_column_mappings( |
|
data_frames, slug_column_mappings, exclude_columns, filename, cur_df |
|
) |
|
|
|
|
|
combined_df = get_combined_df( |
|
data_frames, |
|
[ |
|
"RegionID", |
|
"SizeRank", |
|
"RegionName", |
|
"RegionType", |
|
"StateName", |
|
"Home Type", |
|
"Date", |
|
], |
|
) |
|
|
|
combined_df |
|
|
|
|
|
|
|
|
|
|
|
final_df = combined_df |
|
|
|
for index, row in final_df.iterrows(): |
|
if row["RegionType"] == "city": |
|
final_df.at[index, "City"] = row["RegionName"] |
|
elif row["RegionType"] == "county": |
|
final_df.at[index, "County"] = row["RegionName"] |
|
|
|
|
|
final_df["State"] = final_df["State"].combine_first(final_df["StateName"]) |
|
final_df["State"] = final_df["County"].combine_first(final_df["CountyName"]) |
|
|
|
final_df = final_df.drop(columns=["StateName", "CountyName"]) |
|
final_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
final_df = final_df.rename( |
|
columns={ |
|
"RegionID": "Region ID", |
|
"SizeRank": "Size Rank", |
|
"RegionName": "Region", |
|
"RegionType": "Region Type", |
|
"StateCodeFIPS": "State Code FIPS", |
|
"MunicipalCodeFIPS": "Municipal Code FIPS", |
|
} |
|
) |
|
|
|
final_df |
|
|
|
|
|
|
|
|
|
|
|
save_final_df_as_jsonl(FULL_PROCESSED_DIR_PATH, final_df) |
|
|
|
|