{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "\n", "from helpers import get_data_path_for_config, get_combined_df, save_final_df_as_jsonl" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "CONFIG_NAME = \"home_values_forecasts\"" ] }, { "cell_type": "code", "execution_count": 3, "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", "
RegionIDSizeRankRegionNameRegionTypeStateNameStateCityMetroCountyNameBaseDateMonth Over Month % (Smoothed) (Seasonally Adjusted)Quarter Over Quarter % (Smoothed) (Seasonally Adjusted)Year Over Year % (Smoothed) (Seasonally Adjusted)Month Over Month %Quarter Over Quarter %Year Over Year %
05800130490501zipNYNYHoltsvilleNew York-Newark-Jersey City, NY-NJ-PASuffolk County2023-12-31NaNNaNNaN-0.7-0.90.6
15800230490544zipNYNYHoltsvilleNew York-Newark-Jersey City, NY-NJ-PASuffolk County2023-12-31NaNNaNNaN-0.7-0.90.6
25819674401001zipMAMAAgawamSpringfield, MAHampden County2023-12-310.40.93.2-0.60.03.0
35819739111002zipMAMAAmherstSpringfield, MAHampshire County2023-12-310.20.72.7-0.60.02.9
45819888381003zipMAMAAmherstSpringfield, MAHampshire County2023-12-31NaNNaNNaN-0.70.03.4
...................................................
31849827279777972405zipARARJonesboroJonesboro, ARCraighead County2023-12-31NaNNaNNaN-0.70.02.5
318508342133049011437zipNYNYNew YorkNew York-Newark-Jersey City, NY-NJ-PAQueens County2023-12-31NaNNaNNaN-0.7-0.90.6
31851845914636185288zipAZAZTempePhoenix-Mesa-Chandler, AZMaricopa County2023-12-31NaNNaNNaN-1.00.04.5
318528478543999220598zipVAVAArlingtonWashington-Arlington-Alexandria, DC-VA-MD-WVArlington County2023-12-31NaNNaNNaN-0.40.91.2
318538478553049034249zipFLFLSarasotaNorth Port-Sarasota-Bradenton, FLSarasota County2023-12-31NaNNaNNaN-0.9-0.15.4
\n", "

31854 rows × 16 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName State City \\\n", "0 58001 30490 501 zip NY NY Holtsville \n", "1 58002 30490 544 zip NY NY Holtsville \n", "2 58196 7440 1001 zip MA MA Agawam \n", "3 58197 3911 1002 zip MA MA Amherst \n", "4 58198 8838 1003 zip MA MA Amherst \n", "... ... ... ... ... ... ... ... \n", "31849 827279 7779 72405 zip AR AR Jonesboro \n", "31850 834213 30490 11437 zip NY NY New York \n", "31851 845914 6361 85288 zip AZ AZ Tempe \n", "31852 847854 39992 20598 zip VA VA Arlington \n", "31853 847855 30490 34249 zip FL FL Sarasota \n", "\n", " Metro CountyName \\\n", "0 New York-Newark-Jersey City, NY-NJ-PA Suffolk County \n", "1 New York-Newark-Jersey City, NY-NJ-PA Suffolk County \n", "2 Springfield, MA Hampden County \n", "3 Springfield, MA Hampshire County \n", "4 Springfield, MA Hampshire County \n", "... ... ... \n", "31849 Jonesboro, AR Craighead County \n", "31850 New York-Newark-Jersey City, NY-NJ-PA Queens County \n", "31851 Phoenix-Mesa-Chandler, AZ Maricopa County \n", "31852 Washington-Arlington-Alexandria, DC-VA-MD-WV Arlington County \n", "31853 North Port-Sarasota-Bradenton, FL Sarasota County \n", "\n", " BaseDate Month Over Month % (Smoothed) (Seasonally Adjusted) \\\n", "0 2023-12-31 NaN \n", "1 2023-12-31 NaN \n", "2 2023-12-31 0.4 \n", "3 2023-12-31 0.2 \n", "4 2023-12-31 NaN \n", "... ... ... \n", "31849 2023-12-31 NaN \n", "31850 2023-12-31 NaN \n", "31851 2023-12-31 NaN \n", "31852 2023-12-31 NaN \n", "31853 2023-12-31 NaN \n", "\n", " Quarter Over Quarter % (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 0.9 \n", "3 0.7 \n", "4 NaN \n", "... ... \n", "31849 NaN \n", "31850 NaN \n", "31851 NaN \n", "31852 NaN \n", "31853 NaN \n", "\n", " Year Over Year % (Smoothed) (Seasonally Adjusted) Month Over Month % \\\n", "0 NaN -0.7 \n", "1 NaN -0.7 \n", "2 3.2 -0.6 \n", "3 2.7 -0.6 \n", "4 NaN -0.7 \n", "... ... ... \n", "31849 NaN -0.7 \n", "31850 NaN -0.7 \n", "31851 NaN -1.0 \n", "31852 NaN -0.4 \n", "31853 NaN -0.9 \n", "\n", " Quarter Over Quarter % Year Over Year % \n", "0 -0.9 0.6 \n", "1 -0.9 0.6 \n", "2 0.0 3.0 \n", "3 0.0 2.9 \n", "4 0.0 3.4 \n", "... ... ... \n", "31849 0.0 2.5 \n", "31850 -0.9 0.6 \n", "31851 0.0 4.5 \n", "31852 0.9 1.2 \n", "31853 -0.1 5.4 \n", "\n", "[31854 rows x 16 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_frames = []\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", " 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) (Seasonally Adjusted)\" for x in cols\n", " ]\n", " else:\n", " # print('Raw')\n", " cur_df.columns = list(cur_df.columns[:-3]) + cols\n", "\n", " cur_df[\"RegionName\"] = cur_df[\"RegionName\"].astype(str)\n", "\n", " data_frames.append(cur_df)\n", "\n", "\n", "combined_df = get_combined_df(\n", " data_frames,\n", " [\n", " \"RegionID\",\n", " \"RegionType\",\n", " \"SizeRank\",\n", " \"StateName\",\n", " \"BaseDate\",\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", " \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 TypeStateCityMetroCountyDateMonth Over Month % (Smoothed) (Seasonally Adjusted)Quarter Over Quarter % (Smoothed) (Seasonally Adjusted)Year Over Year % (Smoothed) (Seasonally Adjusted)Month Over Month %Quarter Over Quarter %Year Over Year %
05800130490501zipNYHoltsvilleNew York-Newark-Jersey City, NY-NJ-PASuffolk County2023-12-31NaNNaNNaN-0.7-0.90.6
15800230490544zipNYHoltsvilleNew York-Newark-Jersey City, NY-NJ-PASuffolk County2023-12-31NaNNaNNaN-0.7-0.90.6
25819674401001zipMAAgawamSpringfield, MAHampden County2023-12-310.40.93.2-0.60.03.0
35819739111002zipMAAmherstSpringfield, MAHampshire County2023-12-310.20.72.7-0.60.02.9
45819888381003zipMAAmherstSpringfield, MAHampshire County2023-12-31NaNNaNNaN-0.70.03.4
................................................
31849827279777972405zipARJonesboroJonesboro, ARCraighead County2023-12-31NaNNaNNaN-0.70.02.5
318508342133049011437zipNYNew YorkNew York-Newark-Jersey City, NY-NJ-PAQueens County2023-12-31NaNNaNNaN-0.7-0.90.6
31851845914636185288zipAZTempePhoenix-Mesa-Chandler, AZMaricopa County2023-12-31NaNNaNNaN-1.00.04.5
318528478543999220598zipVAArlingtonWashington-Arlington-Alexandria, DC-VA-MD-WVArlington County2023-12-31NaNNaNNaN-0.40.91.2
318538478553049034249zipFLSarasotaNorth Port-Sarasota-Bradenton, FLSarasota County2023-12-31NaNNaNNaN-0.9-0.15.4
\n", "

31854 rows × 15 columns

\n", "
" ], "text/plain": [ " Region ID Size Rank Region Region Type State City \\\n", "0 58001 30490 501 zip NY Holtsville \n", "1 58002 30490 544 zip NY Holtsville \n", "2 58196 7440 1001 zip MA Agawam \n", "3 58197 3911 1002 zip MA Amherst \n", "4 58198 8838 1003 zip MA Amherst \n", "... ... ... ... ... ... ... \n", "31849 827279 7779 72405 zip AR Jonesboro \n", "31850 834213 30490 11437 zip NY New York \n", "31851 845914 6361 85288 zip AZ Tempe \n", "31852 847854 39992 20598 zip VA Arlington \n", "31853 847855 30490 34249 zip FL Sarasota \n", "\n", " Metro County \\\n", "0 New York-Newark-Jersey City, NY-NJ-PA Suffolk County \n", "1 New York-Newark-Jersey City, NY-NJ-PA Suffolk County \n", "2 Springfield, MA Hampden County \n", "3 Springfield, MA Hampshire County \n", "4 Springfield, MA Hampshire County \n", "... ... ... \n", "31849 Jonesboro, AR Craighead County \n", "31850 New York-Newark-Jersey City, NY-NJ-PA Queens County \n", "31851 Phoenix-Mesa-Chandler, AZ Maricopa County \n", "31852 Washington-Arlington-Alexandria, DC-VA-MD-WV Arlington County \n", "31853 North Port-Sarasota-Bradenton, FL Sarasota County \n", "\n", " Date Month Over Month % (Smoothed) (Seasonally Adjusted) \\\n", "0 2023-12-31 NaN \n", "1 2023-12-31 NaN \n", "2 2023-12-31 0.4 \n", "3 2023-12-31 0.2 \n", "4 2023-12-31 NaN \n", "... ... ... \n", "31849 2023-12-31 NaN \n", "31850 2023-12-31 NaN \n", "31851 2023-12-31 NaN \n", "31852 2023-12-31 NaN \n", "31853 2023-12-31 NaN \n", "\n", " Quarter Over Quarter % (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 0.9 \n", "3 0.7 \n", "4 NaN \n", "... ... \n", "31849 NaN \n", "31850 NaN \n", "31851 NaN \n", "31852 NaN \n", "31853 NaN \n", "\n", " Year Over Year % (Smoothed) (Seasonally Adjusted) Month Over Month % \\\n", "0 NaN -0.7 \n", "1 NaN -0.7 \n", "2 3.2 -0.6 \n", "3 2.7 -0.6 \n", "4 NaN -0.7 \n", "... ... ... \n", "31849 NaN -0.7 \n", "31850 NaN -0.7 \n", "31851 NaN -1.0 \n", "31852 NaN -0.4 \n", "31853 NaN -0.9 \n", "\n", " Quarter Over Quarter % Year Over Year % \n", "0 -0.9 0.6 \n", "1 -0.9 0.6 \n", "2 0.0 3.0 \n", "3 0.0 2.9 \n", "4 0.0 3.4 \n", "... ... ... \n", "31849 0.0 2.5 \n", "31850 -0.9 0.6 \n", "31851 0.0 4.5 \n", "31852 0.9 1.2 \n", "31853 -0.1 5.4 \n", "\n", "[31854 rows x 15 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adjust columns\n", "final_df = combined_df\n", "final_df = combined_df.drop(\"StateName\", axis=1)\n", "final_df = final_df.rename(\n", " columns={\n", " \"CountyName\": \"County\",\n", " \"BaseDate\": \"Date\",\n", " \"RegionName\": \"Region\",\n", " \"RegionType\": \"Region Type\",\n", " \"RegionID\": \"Region ID\",\n", " \"SizeRank\": \"Size Rank\",\n", " }\n", ")\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[\"Region Type\"] == \"msa\":\n", " regionName = row[\"Region\"]\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[\"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 }