{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os" ] }, { "cell_type": "code", "execution_count": 4, "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": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_values(['Mean Listings Price Cut Amount', 'Median Days on Pending', 'Median Days to Close', 'Percent Listings Price Cut'])\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 TypeDateMean Listings Price Cut Amount (Smoothed)Percent Listings Price CutMean Listings Price Cut AmountPercent Listings Price Cut (Smoothed)Median Days on Pending (Smoothed)Median Days on Pending
01020010United StatescountryNaNSFR2018-01-06NaNNaN13508.368375NaNNaNNaN
11020010United StatescountryNaNSFR2018-01-13NaN0.04904214114.788383NaNNaNNaN
21020010United StatescountryNaNSFR2018-01-20NaN0.04474014326.128956NaNNaNNaN
31020010United StatescountryNaNSFR2018-01-27NaN0.04793013998.585612NaNNaNNaN
41020010United StatescountryNaNSFR2018-02-03NaN0.04762214120.035549NaNNaNNaN
..........................................
586709845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-06NaN0.094017NaN0.037378NaNNaN
586710845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-13NaN0.070175NaN0.043203NaNNaN
586711845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-20NaN0.043478NaN0.054073NaNNaN
586712845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-27NaN0.036697NaN0.061092NaNNaN
586713845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-02-03NaN0.077670NaN0.057005NaNNaN
\n", "

586714 rows × 13 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName \\\n", "0 102001 0 United States country NaN \n", "1 102001 0 United States country NaN \n", "2 102001 0 United States country NaN \n", "3 102001 0 United States country NaN \n", "4 102001 0 United States country NaN \n", "... ... ... ... ... ... \n", "586709 845172 769 Winfield, KS msa KS \n", "586710 845172 769 Winfield, KS msa KS \n", "586711 845172 769 Winfield, KS msa KS \n", "586712 845172 769 Winfield, KS msa KS \n", "586713 845172 769 Winfield, KS msa KS \n", "\n", " Home Type Date \\\n", "0 SFR 2018-01-06 \n", "1 SFR 2018-01-13 \n", "2 SFR 2018-01-20 \n", "3 SFR 2018-01-27 \n", "4 SFR 2018-02-03 \n", "... ... ... \n", "586709 all homes (SFR + Condo) 2024-01-06 \n", "586710 all homes (SFR + Condo) 2024-01-13 \n", "586711 all homes (SFR + Condo) 2024-01-20 \n", "586712 all homes (SFR + Condo) 2024-01-27 \n", "586713 all homes (SFR + Condo) 2024-02-03 \n", "\n", " Mean Listings Price Cut Amount (Smoothed) Percent Listings Price Cut \\\n", "0 NaN NaN \n", "1 NaN 0.049042 \n", "2 NaN 0.044740 \n", "3 NaN 0.047930 \n", "4 NaN 0.047622 \n", "... ... ... \n", "586709 NaN 0.094017 \n", "586710 NaN 0.070175 \n", "586711 NaN 0.043478 \n", "586712 NaN 0.036697 \n", "586713 NaN 0.077670 \n", "\n", " Mean Listings Price Cut Amount Percent Listings Price Cut (Smoothed) \\\n", "0 13508.368375 NaN \n", "1 14114.788383 NaN \n", "2 14326.128956 NaN \n", "3 13998.585612 NaN \n", "4 14120.035549 NaN \n", "... ... ... \n", "586709 NaN 0.037378 \n", "586710 NaN 0.043203 \n", "586711 NaN 0.054073 \n", "586712 NaN 0.061092 \n", "586713 NaN 0.057005 \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": 7, "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", " \"_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", "def get_df(\n", " df, exclude_columns, columns_to_pivot, col_name, smoothed, seasonally_adjusted\n", "):\n", " if smoothed:\n", " col_name += \" (Smoothed)\"\n", " if seasonally_adjusted:\n", " col_name += \" (Seasonally Adjusted)\"\n", "\n", " df = pd.melt(\n", " df,\n", " id_vars=exclude_columns,\n", " value_vars=columns_to_pivot,\n", " var_name=\"Date\",\n", " value_name=col_name,\n", " )\n", " return df\n", "\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", " # skip month files for now since they are redundant\n", " if \"month\" in filename:\n", " continue\n", "\n", " if \"_uc_sfrcondo_\" in filename:\n", " cur_df[\"Home Type\"] = \"all homes (SFR + Condo)\"\n", " # change column type to string\n", " cur_df[\"RegionName\"] = cur_df[\"RegionName\"].astype(str)\n", " elif \"_uc_sfr_\" in filename:\n", " cur_df[\"Home Type\"] = \"SFR\"\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", " seasonally_adjusted = \"_sa_\" in filename\n", "\n", " # iterate over slug column mappings and get df\n", " for slug, col_name in slug_column_mappings.items():\n", " if slug in filename:\n", " cur_df = get_df(\n", " cur_df,\n", " exclude_columns,\n", " columns_to_pivot,\n", " col_name,\n", " smoothed,\n", " seasonally_adjusted,\n", " )\n", "\n", " data_frames.append(cur_df)\n", " break\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", "\n", "# iterate over rows of combined df and coalesce column values across columns that start with \"Median Sale Price\"\n", "columns_to_coalesce = slug_column_mappings.values()\n", "print(columns_to_coalesce)\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": 16, "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 TypeStateNameHome TypeDateMean Listings Price Cut Amount (Smoothed)Percent Listings Price CutMean Listings Price Cut AmountPercent Listings Price Cut (Smoothed)Median Days on Pending (Smoothed)Median Days on Pending
01020010United StatescountryNaNSFR2018-01-06NaNNaN13508.368375NaNNaNNaN
11020010United StatescountryNaNSFR2018-01-13NaN0.04904214114.788383NaNNaNNaN
21020010United StatescountryNaNSFR2018-01-20NaN0.04474014326.128956NaNNaNNaN
31020010United StatescountryNaNSFR2018-01-27NaN0.04793013998.585612NaNNaNNaN
41020010United StatescountryNaNSFR2018-02-03NaN0.04762214120.035549NaNNaNNaN
..........................................
586709845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-06NaN0.094017NaN0.037378NaNNaN
586710845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-13NaN0.070175NaN0.043203NaNNaN
586711845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-20NaN0.043478NaN0.054073NaNNaN
586712845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-01-27NaN0.036697NaN0.061092NaNNaN
586713845172769Winfield, KSmsaKSall homes (SFR + Condo)2024-02-03NaN0.077670NaN0.057005NaNNaN
\n", "

586714 rows × 13 columns

\n", "
" ], "text/plain": [ " Region ID Size Rank Region Region Type StateName \\\n", "0 102001 0 United States country NaN \n", "1 102001 0 United States country NaN \n", "2 102001 0 United States country NaN \n", "3 102001 0 United States country NaN \n", "4 102001 0 United States country NaN \n", "... ... ... ... ... ... \n", "586709 845172 769 Winfield, KS msa KS \n", "586710 845172 769 Winfield, KS msa KS \n", "586711 845172 769 Winfield, KS msa KS \n", "586712 845172 769 Winfield, KS msa KS \n", "586713 845172 769 Winfield, KS msa KS \n", "\n", " Home Type Date \\\n", "0 SFR 2018-01-06 \n", "1 SFR 2018-01-13 \n", "2 SFR 2018-01-20 \n", "3 SFR 2018-01-27 \n", "4 SFR 2018-02-03 \n", "... ... ... \n", "586709 all homes (SFR + Condo) 2024-01-06 \n", "586710 all homes (SFR + Condo) 2024-01-13 \n", "586711 all homes (SFR + Condo) 2024-01-20 \n", "586712 all homes (SFR + Condo) 2024-01-27 \n", "586713 all homes (SFR + Condo) 2024-02-03 \n", "\n", " Mean Listings Price Cut Amount (Smoothed) Percent Listings Price Cut \\\n", "0 NaN NaN \n", "1 NaN 0.049042 \n", "2 NaN 0.044740 \n", "3 NaN 0.047930 \n", "4 NaN 0.047622 \n", "... ... ... \n", "586709 NaN 0.094017 \n", "586710 NaN 0.070175 \n", "586711 NaN 0.043478 \n", "586712 NaN 0.036697 \n", "586713 NaN 0.077670 \n", "\n", " Mean Listings Price Cut Amount Percent Listings Price Cut (Smoothed) \\\n", "0 13508.368375 NaN \n", "1 14114.788383 NaN \n", "2 14326.128956 NaN \n", "3 13998.585612 NaN \n", "4 14120.035549 NaN \n", "... ... ... \n", "586709 NaN 0.037378 \n", "586710 NaN 0.043203 \n", "586711 NaN 0.054073 \n", "586712 NaN 0.061092 \n", "586713 NaN 0.057005 \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": 16, "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", " }\n", ")\n", "\n", "final_df" ] }, { "cell_type": "code", "execution_count": 15, "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 }