{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "\n", "from helpers import get_combined_df, save_final_df_as_jsonl" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = \"../data/\"\n", "PROCESSED_DIR = \"../processed/\"\n", "FACET_DIR = \"home_values_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": 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", "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) (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": 1, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'combined_df' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Adjust columns\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m final_df \u001b[38;5;241m=\u001b[39m \u001b[43mcombined_df\u001b[49m\n\u001b[1;32m 3\u001b[0m final_df \u001b[38;5;241m=\u001b[39m combined_df\u001b[38;5;241m.\u001b[39mdrop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mStateName\u001b[39m\u001b[38;5;124m\"\u001b[39m, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 4\u001b[0m final_df \u001b[38;5;241m=\u001b[39m final_df\u001b[38;5;241m.\u001b[39mrename(\n\u001b[1;32m 5\u001b[0m columns\u001b[38;5;241m=\u001b[39m{\n\u001b[1;32m 6\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCountyName\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCounty\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 11\u001b[0m }\n\u001b[1;32m 12\u001b[0m )\n", "\u001b[0;31mNameError\u001b[0m: name 'combined_df' is not defined" ] } ], "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": 9, "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 }