{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "\n", "from helpers import (\n", " get_combined_df,\n", " save_final_df_as_jsonl,\n", " handle_slug_column_mappings,\n", " set_home_type,\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = \"../data\"\n", "PROCESSED_DIR = \"../processed/\"\n", "FACET_DIR = \"days_on_market/\"\n", "FULL_DATA_DIR_PATH = os.path.join(DATA_DIR, FACET_DIR)\n", "FULL_PROCESSED_DIR_PATH = os.path.join(PROCESSED_DIR, FACET_DIR)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing Metro_med_listings_price_cut_amt_uc_sfr_month.csv\n", "processing Metro_perc_listings_price_cut_uc_sfr_week.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfrcondo_month.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfr_week.csv\n", "processing Metro_med_doz_pending_uc_sfrcondo_month.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfr_sm_month.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfrcondo_sm_month.csv\n", "processing Metro_mean_days_to_close_uc_sfrcondo_week.csv\n", "processing Metro_mean_days_to_close_uc_sfrcondo_month.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfrcondo_sm_month.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfr_week.csv\n", "processing Metro_median_days_to_close_uc_sfrcondo_sm_week.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfr_sm_week.csv\n", "processing Metro_mean_listings_price_cut_perc_uc_sfrcondo_sm_week.csv\n", "processing Metro_perc_listings_price_cut_uc_sfrcondo_week.csv\n", "processing Metro_med_doz_pending_uc_sfrcondo_sm_month.csv\n", "processing Metro_mean_days_to_close_uc_sfrcondo_sm_week.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfrcondo_week.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfr_week.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfrcondo_month.csv\n", "processing Metro_mean_doz_pending_uc_sfrcondo_week.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfrcondo_week.csv\n", "processing Metro_median_days_to_close_uc_sfrcondo_week.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfr_sm_month.csv\n", "processing Metro_mean_doz_pending_uc_sfrcondo_sm_month.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfr_sm_month.csv\n", "processing Metro_perc_listings_price_cut_uc_sfrcondo_sm_week.csv\n", "processing Metro_median_days_to_close_uc_sfrcondo_sm_month.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfr_month.csv\n", "processing Metro_mean_listings_price_cut_perc_uc_sfrcondo_week.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfrcondo_week.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfrcondo_sm_week.csv\n", "processing Metro_mean_days_to_close_uc_sfrcondo_sm_month.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfr_sm_week.csv\n", "processing Metro_mean_doz_pending_uc_sfrcondo_sm_week.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfrcondo_sm_week.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfr_sm_week.csv\n", "processing Metro_perc_listings_price_cut_uc_sfrcondo_sm_month.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfrcondo_month.csv\n", "processing Metro_med_listings_price_cut_amt_uc_sfrcondo_sm_month.csv\n", "processing Metro_med_doz_pending_uc_sfrcondo_sm_week.csv\n", "processing Metro_med_listings_price_cut_perc_uc_sfrcondo_sm_week.csv\n", "processing Metro_perc_listings_price_cut_uc_sfr_month.csv\n", "processing Metro_med_doz_pending_uc_sfrcondo_week.csv\n", "processing Metro_mean_listings_price_cut_perc_uc_sfrcondo_sm_month.csv\n", "processing Metro_perc_listings_price_cut_uc_sfr_sm_month.csv\n", "processing Metro_median_days_to_close_uc_sfrcondo_month.csv\n", "processing Metro_perc_listings_price_cut_uc_sfr_sm_week.csv\n", "processing Metro_mean_listings_price_cut_perc_uc_sfrcondo_month.csv\n", "processing Metro_mean_listings_price_cut_amt_uc_sfr_month.csv\n", "processing Metro_mean_doz_pending_uc_sfrcondo_month.csv\n" ] } ], "source": [ "data_frames = []\n", "\n", "exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Home Type\",\n", "]\n", "\n", "slug_column_mappings = {\n", " \"_mean_listings_price_cut_amt_\": \"Mean Listings Price Cut Amount\",\n", " \"_med_doz_pending_\": \"Median Days on Pending\",\n", " \"_median_days_to_pending_\": \"Median Days to Close\",\n", " \"_perc_listings_price_cut_\": \"Percent Listings Price Cut\",\n", "}\n", "\n", "\n", "for filename in os.listdir(FULL_DATA_DIR_PATH):\n", " if filename.endswith(\".csv\"):\n", " print(\"processing \" + filename)\n", " # skip month files for now since they are redundant\n", " if \"month\" in filename:\n", " continue\n", "\n", " cur_df = pd.read_csv(os.path.join(FULL_DATA_DIR_PATH, filename))\n", "\n", " cur_df[\"RegionName\"] = cur_df[\"RegionName\"].astype(str)\n", " cur_df = set_home_type(cur_df, filename)\n", "\n", " data_frames = handle_slug_column_mappings(\n", " data_frames, slug_column_mappings, exclude_columns, filename, cur_df\n", " )\n", "\n", "\n", "combined_df = get_combined_df(\n", " data_frames,\n", " [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Home Type\",\n", " \"Date\",\n", " ],\n", ")\n", "\n", "combined_df" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Region IDSize RankRegionRegion TypeStateHome TypeDatePercent Listings Price CutMean Listings Price Cut AmountPercent Listings Price Cut (Smoothed)Mean Listings Price Cut Amount (Smoothed)Median Days on Pending (Smoothed)Median Days on Pending
01020010United StatescountryNaNSFR2018-01-06NaN13508.368375NaNNaNNaNNaN
11020010United StatescountryNaNSFR2018-01-130.04904214114.788383NaNNaNNaNNaN
21020010United StatescountryNaNSFR2018-01-200.04474014326.128956NaNNaNNaNNaN
31020010United StatescountryNaNSFR2018-01-270.04793013998.585612NaN13998.585612NaNNaN
41020010United StatescountryNaNSFR2018-02-030.04762214120.0355490.04762214120.035549NaNNaN
..........................................
586709845172769Winfield, KSmsaKSall homes2024-01-060.094017NaN0.037378NaNNaNNaN
586710845172769Winfield, KSmsaKSall homes2024-01-130.070175NaN0.043203NaNNaNNaN
586711845172769Winfield, KSmsaKSall homes2024-01-200.043478NaN0.054073NaNNaNNaN
586712845172769Winfield, KSmsaKSall homes2024-01-270.036697NaN0.061092NaNNaNNaN
586713845172769Winfield, KSmsaKSall homes2024-02-030.077670NaN0.057005NaNNaNNaN
\n", "

586714 rows × 13 columns

\n", "
" ], "text/plain": [ " Region ID Size Rank Region Region Type State Home Type \\\n", "0 102001 0 United States country NaN SFR \n", "1 102001 0 United States country NaN SFR \n", "2 102001 0 United States country NaN SFR \n", "3 102001 0 United States country NaN SFR \n", "4 102001 0 United States country NaN SFR \n", "... ... ... ... ... ... ... \n", "586709 845172 769 Winfield, KS msa KS all homes \n", "586710 845172 769 Winfield, KS msa KS all homes \n", "586711 845172 769 Winfield, KS msa KS all homes \n", "586712 845172 769 Winfield, KS msa KS all homes \n", "586713 845172 769 Winfield, KS msa KS all homes \n", "\n", " Date Percent Listings Price Cut Mean Listings Price Cut Amount \\\n", "0 2018-01-06 NaN 13508.368375 \n", "1 2018-01-13 0.049042 14114.788383 \n", "2 2018-01-20 0.044740 14326.128956 \n", "3 2018-01-27 0.047930 13998.585612 \n", "4 2018-02-03 0.047622 14120.035549 \n", "... ... ... ... \n", "586709 2024-01-06 0.094017 NaN \n", "586710 2024-01-13 0.070175 NaN \n", "586711 2024-01-20 0.043478 NaN \n", "586712 2024-01-27 0.036697 NaN \n", "586713 2024-02-03 0.077670 NaN \n", "\n", " Percent Listings Price Cut (Smoothed) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 0.047622 \n", "... ... \n", "586709 0.037378 \n", "586710 0.043203 \n", "586711 0.054073 \n", "586712 0.061092 \n", "586713 0.057005 \n", "\n", " Mean Listings Price Cut Amount (Smoothed) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 13998.585612 \n", "4 14120.035549 \n", "... ... \n", "586709 NaN \n", "586710 NaN \n", "586711 NaN \n", "586712 NaN \n", "586713 NaN \n", "\n", " Median Days on Pending (Smoothed) Median Days on Pending \n", "0 NaN NaN \n", "1 NaN NaN \n", "2 NaN NaN \n", "3 NaN NaN \n", "4 NaN NaN \n", "... ... ... \n", "586709 NaN NaN \n", "586710 NaN NaN \n", "586711 NaN NaN \n", "586712 NaN NaN \n", "586713 NaN NaN \n", "\n", "[586714 rows x 13 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adjust column names\n", "final_df = combined_df.rename(\n", " columns={\n", " \"RegionID\": \"Region ID\",\n", " \"SizeRank\": \"Size Rank\",\n", " \"RegionName\": \"Region\",\n", " \"RegionType\": \"Region Type\",\n", " \"StateName\": \"State\",\n", " }\n", ")\n", "\n", "final_df[\"Date\"] = pd.to_datetime(final_df[\"Date\"], format=\"%Y-%m-%d\")\n", "\n", "final_df" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "save_final_df_as_jsonl(FULL_PROCESSED_DIR_PATH, final_df)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }