{ "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 = \"new_constructions/\"\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": 38, "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": 38, "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", "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", " 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", " if \"median_sale_price_per_sqft\" 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=\"Median Sale Price per Sqft\",\n", " )\n", " data_frames.append(cur_df)\n", "\n", " elif \"median_sale_price\" 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=\"Median Sale Price\",\n", " )\n", " data_frames.append(cur_df)\n", "\n", " elif \"sales_count\" 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=\"Sales Count\",\n", " )\n", " data_frames.append(cur_df)\n", "\n", "\n", "def get_combined_df(data_frames):\n", " combined_df = None\n", " if len(data_frames) > 1:\n", " # iterate over dataframes and merge or concat\n", " combined_df = data_frames[0]\n", " for i in range(1, len(data_frames)):\n", " cur_df = data_frames[i]\n", " combined_df = pd.merge(\n", " combined_df,\n", " cur_df,\n", " on=[\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Home Type\",\n", " \"Date\",\n", " ],\n", " how=\"outer\",\n", " suffixes=(\"\", \"_\" + str(i)),\n", " )\n", " elif len(data_frames) == 1:\n", " combined_df = data_frames[0]\n", "\n", " return combined_df\n", "\n", "\n", "combined_df = get_combined_df(data_frames)\n", "# iterate over rows of combined df and coalesce column values across columns that start with \"Median Sale Price\"\n", "columns_to_coalesce = [\"Sales Count\", \"Median Sale Price\", \"Median Sale Price per Sqft\"]\n", "\n", "for index, row in combined_df.iterrows():\n", " for col in combined_df.columns:\n", " for column_to_coalesce in columns_to_coalesce:\n", " if column_to_coalesce in col and \"_\" in col:\n", " if not pd.isna(row[col]):\n", " combined_df.at[index, column_to_coalesce] = row[col]\n", "\n", "# remove columns with underscores\n", "combined_df = combined_df[[col for col in combined_df.columns if \"_\" not in col]]\n", "\n", "combined_df" ] }, { "cell_type": "code", "execution_count": 39, "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": 39, "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.sort_values(by=[\"Region ID\", \"Home Type\", \"Date\"])" ] }, { "cell_type": "code", "execution_count": 40, "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 }