{ "cells": [ { "cell_type": "code", "execution_count": 1, "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": 2, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = \"../data\"\n", "PROCESSED_DIR = \"../processed/\"\n", "FACET_DIR = \"new_construction/\"\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": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing Metro_new_con_sales_count_raw_uc_condo_month.csv\n", "processing Metro_new_con_median_sale_price_per_sqft_uc_sfr_month.csv\n", "processing Metro_new_con_sales_count_raw_uc_sfr_month.csv\n", "processing Metro_new_con_median_sale_price_uc_sfrcondo_month.csv\n", "processing Metro_new_con_median_sale_price_per_sqft_uc_condo_month.csv\n", "processing Metro_new_con_sales_count_raw_uc_sfrcondo_month.csv\n", "processing Metro_new_con_median_sale_price_uc_condo_month.csv\n", "processing Metro_new_con_median_sale_price_uc_sfr_month.csv\n", "processing Metro_new_con_median_sale_price_per_sqft_uc_sfrcondo_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", "
RegionIDSizeRankRegionNameRegionTypeStateNameHome TypeDateSales CountMedian Sale Price per SqftMedian Sale Price
01020010United StatescountryNaNSFR2018-01-3133940.0137.412316309000.0
11020010United StatescountryNaNSFR2018-02-2833304.0137.199170309072.5
21020010United StatescountryNaNSFR2018-03-3142641.0139.520863315488.0
31020010United StatescountryNaNSFR2018-04-3037588.0139.778110314990.0
41020010United StatescountryNaNSFR2018-05-3139933.0143.317968324500.0
.................................
49482845162535Granbury, TXmsaTXall homes2023-07-3131.0NaNNaN
49483845162535Granbury, TXmsaTXall homes2023-08-3133.0NaNNaN
49484845162535Granbury, TXmsaTXall homes2023-09-3026.0NaNNaN
49485845162535Granbury, TXmsaTXall homes2023-10-3124.0NaNNaN
49486845162535Granbury, TXmsaTXall homes2023-11-3016.0NaNNaN
\n", "

49487 rows × 10 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", "49482 845162 535 Granbury, TX msa TX all homes \n", "49483 845162 535 Granbury, TX msa TX all homes \n", "49484 845162 535 Granbury, TX msa TX all homes \n", "49485 845162 535 Granbury, TX msa TX all homes \n", "49486 845162 535 Granbury, TX msa TX all homes \n", "\n", " Date Sales Count Median Sale Price per Sqft Median Sale Price \n", "0 2018-01-31 33940.0 137.412316 309000.0 \n", "1 2018-02-28 33304.0 137.199170 309072.5 \n", "2 2018-03-31 42641.0 139.520863 315488.0 \n", "3 2018-04-30 37588.0 139.778110 314990.0 \n", "4 2018-05-31 39933.0 143.317968 324500.0 \n", "... ... ... ... ... \n", "49482 2023-07-31 31.0 NaN NaN \n", "49483 2023-08-31 33.0 NaN NaN \n", "49484 2023-09-30 26.0 NaN NaN \n", "49485 2023-10-31 24.0 NaN NaN \n", "49486 2023-11-30 16.0 NaN NaN \n", "\n", "[49487 rows x 10 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Home Type\",\n", "]\n", "\n", "slug_column_mappings = {\n", " \"_median_sale_price_per_sqft\": \"Median Sale Price per Sqft\",\n", " \"_median_sale_price\": \"Median Sale Price\",\n", " \"sales_count\": \"Sales Count\",\n", "}\n", "\n", "data_frames = []\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", " 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", "
Region IDSize RankRegionRegion TypeStateHome TypeDateSales CountMedian Sale Price per SqftMedian Sale Price
01020010United StatescountryNaNSFR2018-01-3133940.0137.412316309000.0
11020010United StatescountryNaNSFR2018-02-2833304.0137.199170309072.5
21020010United StatescountryNaNSFR2018-03-3142641.0139.520863315488.0
31020010United StatescountryNaNSFR2018-04-3037588.0139.778110314990.0
41020010United StatescountryNaNSFR2018-05-3139933.0143.317968324500.0
.................................
49482845162535Granbury, TXmsaTXall homes2023-07-3131.0NaNNaN
49483845162535Granbury, TXmsaTXall homes2023-08-3133.0NaNNaN
49484845162535Granbury, TXmsaTXall homes2023-09-3026.0NaNNaN
49485845162535Granbury, TXmsaTXall homes2023-10-3124.0NaNNaN
49486845162535Granbury, TXmsaTXall homes2023-11-3016.0NaNNaN
\n", "

49487 rows × 10 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", "49482 845162 535 Granbury, TX msa TX all homes \n", "49483 845162 535 Granbury, TX msa TX all homes \n", "49484 845162 535 Granbury, TX msa TX all homes \n", "49485 845162 535 Granbury, TX msa TX all homes \n", "49486 845162 535 Granbury, TX msa TX all homes \n", "\n", " Date Sales Count Median Sale Price per Sqft Median Sale Price \n", "0 2018-01-31 33940.0 137.412316 309000.0 \n", "1 2018-02-28 33304.0 137.199170 309072.5 \n", "2 2018-03-31 42641.0 139.520863 315488.0 \n", "3 2018-04-30 37588.0 139.778110 314990.0 \n", "4 2018-05-31 39933.0 143.317968 324500.0 \n", "... ... ... ... ... \n", "49482 2023-07-31 31.0 NaN NaN \n", "49483 2023-08-31 33.0 NaN NaN \n", "49484 2023-09-30 26.0 NaN NaN \n", "49485 2023-10-31 24.0 NaN NaN \n", "49486 2023-11-30 16.0 NaN NaN \n", "\n", "[49487 rows x 10 columns]" ] }, "execution_count": 4, "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[\"Date\"] = pd.to_datetime(final_df[\"Date\"], format=\"%Y-%m-%d\")\n", "\n", "final_df.sort_values(by=[\"Region ID\", \"Home Type\", \"Date\"])" ] }, { "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 }