{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "\n", "from helpers import (\n", " get_data_path_for_config,\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": 2, "metadata": {}, "outputs": [], "source": [ "CONFIG_NAME = \"for_sale_listings\"" ] }, { "cell_type": "code", "execution_count": 3, "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 Pending (Smoothed)New ListingsNew Listings (Smoothed)New Pending
01020010United StatescountryNaNSFR2018-01-13259000.0NaNNaNNaNNaNNaN
11020010United StatescountryNaNSFR2018-01-20259900.0NaNNaNNaNNaNNaN
21020010United StatescountryNaNSFR2018-01-27259900.0NaNNaNNaNNaNNaN
31020010United StatescountryNaNSFR2018-02-03260000.0259700.0NaNNaNNaNNaN
41020010United StatescountryNaNSFR2018-02-10264900.0261175.0NaNNaNNaNNaN
..........................................
578648845172769Winfield, KSmsaKSall homes2023-12-09134950.0138913.0NaNNaNNaNNaN
578649845172769Winfield, KSmsaKSall homes2023-12-16120000.0133938.0NaNNaNNaNNaN
578650845172769Winfield, KSmsaKSall homes2023-12-23111000.0126463.0NaNNaNNaNNaN
578651845172769Winfield, KSmsaKSall homes2023-12-30126950.0123225.0NaNNaNNaNNaN
578652845172769Winfield, KSmsaKSall homes2024-01-06128000.0121488.0NaNNaNNaNNaN
\n", "

578653 rows × 13 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName 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", "578648 845172 769 Winfield, KS msa KS all homes \n", "578649 845172 769 Winfield, KS msa KS all homes \n", "578650 845172 769 Winfield, KS msa KS all homes \n", "578651 845172 769 Winfield, KS msa KS all homes \n", "578652 845172 769 Winfield, KS msa KS all homes \n", "\n", " Date Median Listing Price Median Listing Price (Smoothed) \\\n", "0 2018-01-13 259000.0 NaN \n", "1 2018-01-20 259900.0 NaN \n", "2 2018-01-27 259900.0 NaN \n", "3 2018-02-03 260000.0 259700.0 \n", "4 2018-02-10 264900.0 261175.0 \n", "... ... ... ... \n", "578648 2023-12-09 134950.0 138913.0 \n", "578649 2023-12-16 120000.0 133938.0 \n", "578650 2023-12-23 111000.0 126463.0 \n", "578651 2023-12-30 126950.0 123225.0 \n", "578652 2024-01-06 128000.0 121488.0 \n", "\n", " New Pending (Smoothed) New Listings New Listings (Smoothed) \\\n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 NaN NaN NaN \n", "3 NaN NaN NaN \n", "4 NaN NaN NaN \n", "... ... ... ... \n", "578648 NaN NaN NaN \n", "578649 NaN NaN NaN \n", "578650 NaN NaN NaN \n", "578651 NaN NaN NaN \n", "578652 NaN NaN NaN \n", "\n", " New Pending \n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "578648 NaN \n", "578649 NaN \n", "578650 NaN \n", "578651 NaN \n", "578652 NaN \n", "\n", "[578653 rows x 13 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "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", " \"_mlp_\": \"Median Listing Price\",\n", " \"_new_listings_\": \"New Listings\",\n", " \"new_pending\": \"New Pending\",\n", "}\n", "\n", "data_dir_path = get_data_path_for_config(CONFIG_NAME)\n", "\n", "for filename in os.listdir(data_dir_path):\n", " if filename.endswith(\".csv\"):\n", " print(\"processing \" + filename)\n", " cur_df = pd.read_csv(os.path.join(data_dir_path, filename))\n", "\n", " # ignore monthly data for now since it is redundant\n", " if \"month\" in filename:\n", " continue\n", "\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", "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 TypeDateMedian Listing PriceMedian Listing Price (Smoothed)New Pending (Smoothed)New ListingsNew Listings (Smoothed)New Pending
01020010United StatescountryNaNSFR2018-01-13259000.0NaNNaNNaNNaNNaN
11020010United StatescountryNaNSFR2018-01-20259900.0NaNNaNNaNNaNNaN
21020010United StatescountryNaNSFR2018-01-27259900.0NaNNaNNaNNaNNaN
31020010United StatescountryNaNSFR2018-02-03260000.0259700.0NaNNaNNaNNaN
41020010United StatescountryNaNSFR2018-02-10264900.0261175.0NaNNaNNaNNaN
..........................................
578648845172769Winfield, KSmsaKSall homes2023-12-09134950.0138913.0NaNNaNNaNNaN
578649845172769Winfield, KSmsaKSall homes2023-12-16120000.0133938.0NaNNaNNaNNaN
578650845172769Winfield, KSmsaKSall homes2023-12-23111000.0126463.0NaNNaNNaNNaN
578651845172769Winfield, KSmsaKSall homes2023-12-30126950.0123225.0NaNNaNNaNNaN
578652845172769Winfield, KSmsaKSall homes2024-01-06128000.0121488.0NaNNaNNaNNaN
\n", "

578653 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", "578648 845172 769 Winfield, KS msa KS all homes \n", "578649 845172 769 Winfield, KS msa KS all homes \n", "578650 845172 769 Winfield, KS msa KS all homes \n", "578651 845172 769 Winfield, KS msa KS all homes \n", "578652 845172 769 Winfield, KS msa KS all homes \n", "\n", " Date Median Listing Price Median Listing Price (Smoothed) \\\n", "0 2018-01-13 259000.0 NaN \n", "1 2018-01-20 259900.0 NaN \n", "2 2018-01-27 259900.0 NaN \n", "3 2018-02-03 260000.0 259700.0 \n", "4 2018-02-10 264900.0 261175.0 \n", "... ... ... ... \n", "578648 2023-12-09 134950.0 138913.0 \n", "578649 2023-12-16 120000.0 133938.0 \n", "578650 2023-12-23 111000.0 126463.0 \n", "578651 2023-12-30 126950.0 123225.0 \n", "578652 2024-01-06 128000.0 121488.0 \n", "\n", " New Pending (Smoothed) New Listings New Listings (Smoothed) \\\n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 NaN NaN NaN \n", "3 NaN NaN NaN \n", "4 NaN NaN NaN \n", "... ... ... ... \n", "578648 NaN NaN NaN \n", "578649 NaN NaN NaN \n", "578650 NaN NaN NaN \n", "578651 NaN NaN NaN \n", "578652 NaN NaN NaN \n", "\n", " New Pending \n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "578648 NaN \n", "578649 NaN \n", "578650 NaN \n", "578651 NaN \n", "578652 NaN \n", "\n", "[578653 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(CONFIG_NAME, 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 }