{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = \"../data/\"\n", "PROCESSED_DIR = \"../processed/\"\n", "FACET_DIR = \"home_value_forecasts/\"\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": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing Zip_zhvf_growth_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvf_growth_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Zip_zhvf_growth_uc_sfrcondo_tier_0.33_0.67_month.csv\n", "processing Metro_zhvf_growth_uc_sfrcondo_tier_0.33_0.67_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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
RegionIDSizeRankRegionNameRegionTypeStateNameBaseDateMonth Over Month % (Smoothed)Quarter Over Quarter % (Smoothed)Year Over Year % (Smoothed)Month Over Month % (Raw)Quarter Over Quarter % (Raw)Year Over Year % (Raw)StateCityMetroCountyName
01020010United StatescountryNaN2023-12-310.10.43.5-0.50.43.7NaNNaNNaNNaN
13949131New York, NYmsaNY2023-12-310.20.21.0-0.7-0.90.6NaNNaNNaNNaN
27538992Los Angeles, CAmsaCA2023-12-31-0.1-1.80.7-0.60.81.4NaNNaNNaNNaN
33944633Chicago, ILmsaIL2023-12-310.10.41.6-0.8-0.21.4NaNNaNNaNNaN
43945144Dallas, TXmsaTX2023-12-31-0.10.03.2-0.60.93.6NaNNaNNaNNaN
...................................................
20162820973999255087zipMN2023-12-310.10.71.8-0.9-0.22.6MNWarsawFaribault-Northfield, MNRice County
20163853253999262093zipIL2023-12-310.90.43.7-0.70.42.3ILNaNSt. Louis, MO-ILMacoupin County
20164920853999277661zipTX2023-12-31-0.50.3-0.6-0.40.01.2TXNaNHouston-The Woodlands-Sugar Land, TXChambers County
20165928113999279078zipTX2023-12-31-1.2-1.1-3.1-1.7-2.6-1.9TXNaNBorger, TXHutchinson County
20166981833999295419zipCA2023-12-31-0.5-0.20.0-0.50.6-0.4CACamp MeekerSanta Rosa-Petaluma, CASonoma County
\n", "

21062 rows × 16 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName BaseDate \\\n", "0 102001 0 United States country NaN 2023-12-31 \n", "1 394913 1 New York, NY msa NY 2023-12-31 \n", "2 753899 2 Los Angeles, CA msa CA 2023-12-31 \n", "3 394463 3 Chicago, IL msa IL 2023-12-31 \n", "4 394514 4 Dallas, TX msa TX 2023-12-31 \n", "... ... ... ... ... ... ... \n", "20162 82097 39992 55087 zip MN 2023-12-31 \n", "20163 85325 39992 62093 zip IL 2023-12-31 \n", "20164 92085 39992 77661 zip TX 2023-12-31 \n", "20165 92811 39992 79078 zip TX 2023-12-31 \n", "20166 98183 39992 95419 zip CA 2023-12-31 \n", "\n", " Month Over Month % (Smoothed) Quarter Over Quarter % (Smoothed) \\\n", "0 0.1 0.4 \n", "1 0.2 0.2 \n", "2 -0.1 -1.8 \n", "3 0.1 0.4 \n", "4 -0.1 0.0 \n", "... ... ... \n", "20162 0.1 0.7 \n", "20163 0.9 0.4 \n", "20164 -0.5 0.3 \n", "20165 -1.2 -1.1 \n", "20166 -0.5 -0.2 \n", "\n", " Year Over Year % (Smoothed) Month Over Month % (Raw) \\\n", "0 3.5 -0.5 \n", "1 1.0 -0.7 \n", "2 0.7 -0.6 \n", "3 1.6 -0.8 \n", "4 3.2 -0.6 \n", "... ... ... \n", "20162 1.8 -0.9 \n", "20163 3.7 -0.7 \n", "20164 -0.6 -0.4 \n", "20165 -3.1 -1.7 \n", "20166 0.0 -0.5 \n", "\n", " Quarter Over Quarter % (Raw) Year Over Year % (Raw) State \\\n", "0 0.4 3.7 NaN \n", "1 -0.9 0.6 NaN \n", "2 0.8 1.4 NaN \n", "3 -0.2 1.4 NaN \n", "4 0.9 3.6 NaN \n", "... ... ... ... \n", "20162 -0.2 2.6 MN \n", "20163 0.4 2.3 IL \n", "20164 0.0 1.2 TX \n", "20165 -2.6 -1.9 TX \n", "20166 0.6 -0.4 CA \n", "\n", " City Metro CountyName \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", "20162 Warsaw Faribault-Northfield, MN Rice County \n", "20163 NaN St. Louis, MO-IL Macoupin County \n", "20164 NaN Houston-The Woodlands-Sugar Land, TX Chambers County \n", "20165 NaN Borger, TX Hutchinson County \n", "20166 Camp Meeker Santa Rosa-Petaluma, CA Sonoma County \n", "\n", "[21062 rows x 16 columns]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "metro_data_frames = []\n", "zip_data_frames = []\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", " cols = [\"Month Over Month %\", \"Quarter Over Quarter %\", \"Year Over Year %\"]\n", " if filename.endswith(\"sm_sa_month.csv\"):\n", " # print('Smoothed')\n", " cur_df.columns = list(cur_df.columns[:-3]) + [\n", " x + \" (Smoothed)\" for x in cols\n", " ]\n", " else:\n", " # print('Raw')\n", " cur_df.columns = list(cur_df.columns[:-3]) + [x + \" (Raw)\" for x in cols]\n", "\n", " if filename.startswith(\"Metro\"):\n", " # print('Metro')\n", " metro_data_frames.append(cur_df)\n", "\n", " elif filename.startswith(\"Zip\"):\n", " # print('Zip')\n", " zip_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 them\n", " final_df = data_frames[0]\n", " for i in range(1, len(data_frames)):\n", " cur_df = data_frames[i]\n", " cols = list(cur_df.columns[-3:])\n", " cols.append(\"RegionID\")\n", " combined_df = pd.merge(final_df, cur_df[cols], on=\"RegionID\")\n", " elif len(data_frames) == 1:\n", " combined_df = data_frames[0]\n", "\n", " return combined_df\n", "\n", "\n", "combined_metro_dfs = get_combined_df(metro_data_frames)\n", "combined_zip_dfs = get_combined_df(zip_data_frames)\n", "\n", "combined_df = pd.concat([combined_metro_dfs, combined_zip_dfs])\n", "combined_df" ] }, { "cell_type": "code", "execution_count": 12, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
RegionIDRegionNameRegionTypeSizeRankStateCityMetroCountyBaseDateMonth Over Month % (Smoothed)Quarter Over Quarter % (Smoothed)Year Over Year % (Smoothed)Month Over Month % (Raw)Quarter Over Quarter % (Raw)Year Over Year % (Raw)
0102001United Statescountry0NaNNaNNaNNaN2023-12-310.10.43.5-0.50.43.7
1394913New York, NYmsa1NYNew YorkNaNNaN2023-12-310.20.21.0-0.7-0.90.6
2753899Los Angeles, CAmsa2CALos AngelesNaNNaN2023-12-31-0.1-1.80.7-0.60.81.4
3394463Chicago, ILmsa3ILChicagoNaNNaN2023-12-310.10.41.6-0.8-0.21.4
4394514Dallas, TXmsa4TXDallasNaNNaN2023-12-31-0.10.03.2-0.60.93.6
................................................
201628209755087zip39992MNWarsawFaribault-Northfield, MNRice County2023-12-310.10.71.8-0.9-0.22.6
201638532562093zip39992ILNaNSt. Louis, MO-ILMacoupin County2023-12-310.90.43.7-0.70.42.3
201649208577661zip39992TXNaNHouston-The Woodlands-Sugar Land, TXChambers County2023-12-31-0.50.3-0.6-0.40.01.2
201659281179078zip39992TXNaNBorger, TXHutchinson County2023-12-31-1.2-1.1-3.1-1.7-2.6-1.9
201669818395419zip39992CACamp MeekerSanta Rosa-Petaluma, CASonoma County2023-12-31-0.5-0.20.0-0.50.6-0.4
\n", "

21062 rows × 15 columns

\n", "
" ], "text/plain": [ " RegionID RegionName RegionType SizeRank State City \\\n", "0 102001 United States country 0 NaN NaN \n", "1 394913 New York, NY msa 1 NY New York \n", "2 753899 Los Angeles, CA msa 2 CA Los Angeles \n", "3 394463 Chicago, IL msa 3 IL Chicago \n", "4 394514 Dallas, TX msa 4 TX Dallas \n", "... ... ... ... ... ... ... \n", "20162 82097 55087 zip 39992 MN Warsaw \n", "20163 85325 62093 zip 39992 IL NaN \n", "20164 92085 77661 zip 39992 TX NaN \n", "20165 92811 79078 zip 39992 TX NaN \n", "20166 98183 95419 zip 39992 CA Camp Meeker \n", "\n", " Metro County BaseDate \\\n", "0 NaN NaN 2023-12-31 \n", "1 NaN NaN 2023-12-31 \n", "2 NaN NaN 2023-12-31 \n", "3 NaN NaN 2023-12-31 \n", "4 NaN NaN 2023-12-31 \n", "... ... ... ... \n", "20162 Faribault-Northfield, MN Rice County 2023-12-31 \n", "20163 St. Louis, MO-IL Macoupin County 2023-12-31 \n", "20164 Houston-The Woodlands-Sugar Land, TX Chambers County 2023-12-31 \n", "20165 Borger, TX Hutchinson County 2023-12-31 \n", "20166 Santa Rosa-Petaluma, CA Sonoma County 2023-12-31 \n", "\n", " Month Over Month % (Smoothed) Quarter Over Quarter % (Smoothed) \\\n", "0 0.1 0.4 \n", "1 0.2 0.2 \n", "2 -0.1 -1.8 \n", "3 0.1 0.4 \n", "4 -0.1 0.0 \n", "... ... ... \n", "20162 0.1 0.7 \n", "20163 0.9 0.4 \n", "20164 -0.5 0.3 \n", "20165 -1.2 -1.1 \n", "20166 -0.5 -0.2 \n", "\n", " Year Over Year % (Smoothed) Month Over Month % (Raw) \\\n", "0 3.5 -0.5 \n", "1 1.0 -0.7 \n", "2 0.7 -0.6 \n", "3 1.6 -0.8 \n", "4 3.2 -0.6 \n", "... ... ... \n", "20162 1.8 -0.9 \n", "20163 3.7 -0.7 \n", "20164 -0.6 -0.4 \n", "20165 -3.1 -1.7 \n", "20166 0.0 -0.5 \n", "\n", " Quarter Over Quarter % (Raw) Year Over Year % (Raw) \n", "0 0.4 3.7 \n", "1 -0.9 0.6 \n", "2 0.8 1.4 \n", "3 -0.2 1.4 \n", "4 0.9 3.6 \n", "... ... ... \n", "20162 -0.2 2.6 \n", "20163 0.4 2.3 \n", "20164 0.0 1.2 \n", "20165 -2.6 -1.9 \n", "20166 0.6 -0.4 \n", "\n", "[21062 rows x 15 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cols = list(combined_df.columns)\n", "result_cols = [x for x in cols if \"%\" in x]\n", "\n", "all_cols = [\n", " \"RegionID\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"SizeRank\",\n", " \"StateName\",\n", " \"State\",\n", " \"City\",\n", " \"Metro\",\n", " \"CountyName\",\n", " \"BaseDate\",\n", "] + result_cols\n", "\n", "final_df = combined_df[all_cols]\n", "final_df = final_df.drop(\"StateName\", axis=1)\n", "final_df = final_df.rename(columns={\"CountyName\": \"County\"})\n", "\n", "# iterate over rows of final_df and populate State and City columns if the regionType is msa\n", "for index, row in final_df.iterrows():\n", " if row[\"RegionType\"] == \"msa\":\n", " regionName = row[\"RegionName\"]\n", " # final_df.at[index, 'Metro'] = regionName\n", "\n", " city = regionName.split(\", \")[0]\n", " final_df.at[index, \"City\"] = city\n", "\n", " state = regionName.split(\", \")[1]\n", " final_df.at[index, \"State\"] = state\n", "\n", "final_df" ] }, { "cell_type": "code", "execution_count": 13, "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 }