{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = \"../data\"\n", "PROCESSED_DIR = \"../processed/\"\n", "FACET_DIR = \"for_sale_listings/\"\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": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing Metro_new_pending_uc_sfrcondo_sm_month.csv\n", "processing Metro_invt_fs_uc_sfrcondo_week.csv\n", "processing Metro_mlp_uc_sfrcondo_week.csv\n", "processing Metro_invt_fs_uc_sfr_month.csv\n", "processing Metro_mlp_uc_sfr_sm_month.csv\n", "processing Metro_new_pending_uc_sfrcondo_month.csv\n", "processing Metro_mlp_uc_sfrcondo_sm_week.csv\n", "processing Metro_invt_fs_uc_sfrcondo_month.csv\n", "processing Metro_mlp_uc_sfr_sm_week.csv\n", "processing Metro_mlp_uc_sfrcondo_month.csv\n", "processing Metro_new_pending_uc_sfrcondo_sm_week.csv\n", "processing Metro_invt_fs_uc_sfr_sm_week.csv\n", "processing Metro_invt_fs_uc_sfr_sm_month.csv\n", "processing Metro_mlp_uc_sfr_month.csv\n", "processing Metro_new_listings_uc_sfrcondo_week.csv\n", "processing Metro_mlp_uc_sfrcondo_sm_month.csv\n", "processing Metro_invt_fs_uc_sfrcondo_sm_week.csv\n", "processing Metro_new_listings_uc_sfrcondo_sm_week.csv\n", "processing Metro_new_listings_uc_sfrcondo_month.csv\n", "processing Metro_new_pending_uc_sfrcondo_week.csv\n", "processing Metro_invt_fs_uc_sfr_week.csv\n", "processing Metro_new_listings_uc_sfrcondo_sm_month.csv\n", "processing Metro_mlp_uc_sfr_week.csv\n", "processing Metro_invt_fs_uc_sfrcondo_sm_month.csv\n" ] }, { "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", "
RegionIDSizeRankRegionNameRegionTypeStateNameHome TypeDateMedian Listing PriceMedian Listing Price (Smoothed)New ListingsNew Listings (Smoothed)New Pending (Smoothed)New Pending
01020010United StatescountryNaNall homes2018-01-06NaNNaNNaNNaNNaN24766.0
11020010United StatescountryNaNSFR2018-01-13259000.0NaNNaNNaNNaNNaN
21020010United StatescountryNaNall homes2018-01-13259900.0NaN71177.0NaNNaN35229.0
31020010United StatescountryNaNSFR2018-01-20259900.0NaNNaNNaNNaNNaN
41020010United StatescountryNaNall homes2018-01-20259900.0NaN72625.0NaNNaN38281.0
..........................................
2398144845172769Winfield, KSmsaKSall homes2023-12-31NaN136233.0NaN28.0NaN24.0
2398145845172769Winfield, KSmsaKSSFR2024-01-06NaN131088.0NaNNaNNaNNaN
2398146845172769Winfield, KSmsaKSSFR2024-01-06135450.0NaNNaNNaNNaNNaN
2398147845172769Winfield, KSmsaKSall homes2024-01-06128000.0NaNNaNNaNNaNNaN
2398148845172769Winfield, KSmsaKSall homes2024-01-06NaN121488.0NaNNaNNaNNaN
\n", "

2398149 rows × 13 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName Home Type \\\n", "0 102001 0 United States country NaN all homes \n", "1 102001 0 United States country NaN SFR \n", "2 102001 0 United States country NaN all homes \n", "3 102001 0 United States country NaN SFR \n", "4 102001 0 United States country NaN all homes \n", "... ... ... ... ... ... ... \n", "2398144 845172 769 Winfield, KS msa KS all homes \n", "2398145 845172 769 Winfield, KS msa KS SFR \n", "2398146 845172 769 Winfield, KS msa KS SFR \n", "2398147 845172 769 Winfield, KS msa KS all homes \n", "2398148 845172 769 Winfield, KS msa KS all homes \n", "\n", " Date Median Listing Price Median Listing Price (Smoothed) \\\n", "0 2018-01-06 NaN NaN \n", "1 2018-01-13 259000.0 NaN \n", "2 2018-01-13 259900.0 NaN \n", "3 2018-01-20 259900.0 NaN \n", "4 2018-01-20 259900.0 NaN \n", "... ... ... ... \n", "2398144 2023-12-31 NaN 136233.0 \n", "2398145 2024-01-06 NaN 131088.0 \n", "2398146 2024-01-06 135450.0 NaN \n", "2398147 2024-01-06 128000.0 NaN \n", "2398148 2024-01-06 NaN 121488.0 \n", "\n", " New Listings New Listings (Smoothed) New Pending (Smoothed) \\\n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 71177.0 NaN NaN \n", "3 NaN NaN NaN \n", "4 72625.0 NaN NaN \n", "... ... ... ... \n", "2398144 NaN 28.0 NaN \n", "2398145 NaN NaN NaN \n", "2398146 NaN NaN NaN \n", "2398147 NaN NaN NaN \n", "2398148 NaN NaN NaN \n", "\n", " New Pending \n", "0 24766.0 \n", "1 NaN \n", "2 35229.0 \n", "3 NaN \n", "4 38281.0 \n", "... ... \n", "2398144 24.0 \n", "2398145 NaN \n", "2398146 NaN \n", "2398147 NaN \n", "2398148 NaN \n", "\n", "[2398149 rows x 13 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# base cols RegionID,SizeRank,RegionName,RegionType,StateName\n", "\n", "exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Home Type\",\n", "]\n", "\n", "batches = {\"mlp\": [], \"new_listings\": [], \"new_pending\": []}\n", "\n", "for filename in os.listdir(FULL_DATA_DIR_PATH):\n", " if filename.endswith(\".csv\"):\n", " print(\"processing \" + filename)\n", " cur_df = pd.read_csv(os.path.join(FULL_DATA_DIR_PATH, filename))\n", "\n", " # ignore monthly data for now since it is redundant\n", " if \"monthly\" in filename:\n", " continue\n", "\n", " if \"sfrcondo\" in filename:\n", " cur_df[\"Home Type\"] = \"all homes\"\n", " elif \"sfr\" in filename:\n", " cur_df[\"Home Type\"] = \"SFR\"\n", " elif \"condo\" in filename:\n", " cur_df[\"Home Type\"] = \"condo/co-op only\"\n", "\n", " # Identify columns to pivot\n", " columns_to_pivot = [col for col in cur_df.columns if col not in exclude_columns]\n", "\n", " smoothed = \"_sm_\" in filename\n", "\n", " if \"_mlp_\" in filename:\n", " cur_df = pd.melt(\n", " cur_df,\n", " id_vars=exclude_columns,\n", " value_vars=columns_to_pivot,\n", " var_name=\"Date\",\n", " value_name=(\n", " \"Median Listing Price\"\n", " if not smoothed\n", " else \"Median Listing Price (Smoothed)\"\n", " ),\n", " )\n", " batches[\"mlp\"].append(cur_df)\n", "\n", " elif \"_new_listings_\" in filename:\n", " cur_df = pd.melt(\n", " cur_df,\n", " id_vars=exclude_columns,\n", " value_vars=columns_to_pivot,\n", " var_name=\"Date\",\n", " value_name=(\n", " \"New Listings\" if not smoothed else \"New Listings (Smoothed)\"\n", " ),\n", " )\n", " batches[\"new_listings\"].append(cur_df)\n", "\n", " elif \"new_pending\" in filename:\n", " cur_df = pd.melt(\n", " cur_df,\n", " id_vars=exclude_columns,\n", " value_vars=columns_to_pivot,\n", " var_name=\"Date\",\n", " value_name=\"New Pending\" if not smoothed else \"New Pending (Smoothed)\",\n", " )\n", " batches[\"new_pending\"].append(cur_df)\n", "\n", "matching_cols = [\n", " \"RegionID\",\n", " \"Date\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Home Type\",\n", "]\n", "\n", "combined_batches = [pd.concat(cur_batch) for cur_batch in batches.values()]\n", "\n", "if len(combined_batches) > 0:\n", " combined_df = combined_batches[0]\n", " for batch in combined_batches[1:]:\n", " combined_df = pd.merge(\n", " combined_df,\n", " batch,\n", " on=matching_cols,\n", " how=\"outer\",\n", " )\n", "\n", "\n", "combined_df" ] }, { "cell_type": "code", "execution_count": 6, "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 TypeDateMedian Listing PriceMedian Listing Price (Smoothed)New ListingsNew Listings (Smoothed)New Pending (Smoothed)New Pending
01020010United StatescountryNaNall homes2018-01-06NaNNaNNaNNaNNaN24766.0
11020010United StatescountryNaNSFR2018-01-13259000.0NaNNaNNaNNaNNaN
21020010United StatescountryNaNall homes2018-01-13259900.0NaN71177.0NaNNaN35229.0
31020010United StatescountryNaNSFR2018-01-20259900.0NaNNaNNaNNaNNaN
41020010United StatescountryNaNall homes2018-01-20259900.0NaN72625.0NaNNaN38281.0
..........................................
2398144845172769Winfield, KSmsaKSall homes2023-12-31NaN136233.0NaN28.0NaN24.0
2398145845172769Winfield, KSmsaKSSFR2024-01-06NaN131088.0NaNNaNNaNNaN
2398146845172769Winfield, KSmsaKSSFR2024-01-06135450.0NaNNaNNaNNaNNaN
2398147845172769Winfield, KSmsaKSall homes2024-01-06128000.0NaNNaNNaNNaNNaN
2398148845172769Winfield, KSmsaKSall homes2024-01-06NaN121488.0NaNNaNNaNNaN
\n", "

2398149 rows × 13 columns

\n", "
" ], "text/plain": [ " Region ID Size Rank Region Region Type State Home Type \\\n", "0 102001 0 United States country NaN all homes \n", "1 102001 0 United States country NaN SFR \n", "2 102001 0 United States country NaN all homes \n", "3 102001 0 United States country NaN SFR \n", "4 102001 0 United States country NaN all homes \n", "... ... ... ... ... ... ... \n", "2398144 845172 769 Winfield, KS msa KS all homes \n", "2398145 845172 769 Winfield, KS msa KS SFR \n", "2398146 845172 769 Winfield, KS msa KS SFR \n", "2398147 845172 769 Winfield, KS msa KS all homes \n", "2398148 845172 769 Winfield, KS msa KS all homes \n", "\n", " Date Median Listing Price Median Listing Price (Smoothed) \\\n", "0 2018-01-06 NaN NaN \n", "1 2018-01-13 259000.0 NaN \n", "2 2018-01-13 259900.0 NaN \n", "3 2018-01-20 259900.0 NaN \n", "4 2018-01-20 259900.0 NaN \n", "... ... ... ... \n", "2398144 2023-12-31 NaN 136233.0 \n", "2398145 2024-01-06 NaN 131088.0 \n", "2398146 2024-01-06 135450.0 NaN \n", "2398147 2024-01-06 128000.0 NaN \n", "2398148 2024-01-06 NaN 121488.0 \n", "\n", " New Listings New Listings (Smoothed) New Pending (Smoothed) \\\n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 71177.0 NaN NaN \n", "3 NaN NaN NaN \n", "4 72625.0 NaN NaN \n", "... ... ... ... \n", "2398144 NaN 28.0 NaN \n", "2398145 NaN NaN NaN \n", "2398146 NaN NaN NaN \n", "2398147 NaN NaN NaN \n", "2398148 NaN NaN NaN \n", "\n", " New Pending \n", "0 24766.0 \n", "1 NaN \n", "2 35229.0 \n", "3 NaN \n", "4 38281.0 \n", "... ... \n", "2398144 24.0 \n", "2398145 NaN \n", "2398146 NaN \n", "2398147 NaN \n", "2398148 NaN \n", "\n", "[2398149 rows x 13 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_df = combined_df\n", "final_df = final_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" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "if not os.path.exists(FULL_PROCESSED_DIR_PATH):\n", " os.makedirs(FULL_PROCESSED_DIR_PATH)\n", "\n", "final_df.to_json(FULL_PROCESSED_DIR_PATH + \"final.jsonl\", orient=\"records\", lines=True)" ] } ], "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 }