{ "cells": [ { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = \"../data\"\n", "PROCESSED_DIR = \"../processed/\"\n", "FACET_DIR = \"home_values/\"\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": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing City_zhvi_uc_condo_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_uc_sfrcondo_tier_0.67_1.0_sm_sa_month.csv\n", "processing County_zhvi_uc_sfrcondo_tier_0.67_1.0_sm_sa_month.csv\n", "processing Metro_zhvi_bdrmcnt_2_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_bdrmcnt_4_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_uc_sfr_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_bdrmcnt_4_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_uc_sfr_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_uc_condo_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_bdrmcnt_4_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_bdrmcnt_5_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Zip_zhvi_bdrmcnt_2_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_uc_sfrcondo_tier_0.67_1.0_sm_sa_month.csv\n", "processing Zip_zhvi_uc_condo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_uc_sfr_sm_sa_month.csv\n", "processing Metro_zhvi_uc_sfr_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Zip_zhvi_bdrmcnt_1_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_bdrmcnt_5_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_uc_condo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_bdrmcnt_3_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_bdrmcnt_5_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_uc_sfrcondo_tier_0.0_0.33_sm_sa_month.csv\n", "processing Metro_zhvi_bdrmcnt_1_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Zip_zhvi_uc_sfr_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_bdrmcnt_5_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_bdrmcnt_4_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_uc_condo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Zip_zhvi_bdrmcnt_3_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_bdrmcnt_1_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_bdrmcnt_3_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_uc_sfr_tier_0.33_0.67_sm_sa_month (1).csv\n", "processing County_zhvi_bdrmcnt_1_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_uc_condo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_uc_sfrcondo_tier_0.33_0.67_month.csv\n", "processing Zip_zhvi_bdrmcnt_5_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_uc_sfrcondo_tier_0.0_0.33_sm_sa_month.csv\n", "processing State_zhvi_bdrmcnt_2_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_uc_sfrcondo_tier_0.0_0.33_sm_sa_month.csv\n", "processing City_zhvi_uc_sfr_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_bdrmcnt_1_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_bdrmcnt_3_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_bdrmcnt_5_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_bdrmcnt_3_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing City_zhvi_bdrmcnt_2_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_uc_sfrcondo_tier_0.67_1.0_sm_sa_month.csv\n", "processing Zip_zhvi_bdrmcnt_4_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_bdrmcnt_3_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing State_zhvi_bdrmcnt_1_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Neighborhood_zhvi_bdrmcnt_2_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing County_zhvi_bdrmcnt_2_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "processing Metro_zhvi_bdrmcnt_4_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv\n", "1\n", "10\n", "2\n", "10\n", "3\n", "10\n", "4\n", "10\n", "5\n", "10\n", "6\n", "10\n", "7\n", "10\n", "8\n", "10\n", "9\n", "10\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
RegionIDSizeRankRegionNameRegionTypeStateNameBedroom CountHome TypeDateMid Tier ZHVI (Smoothed) (Seasonally Adjusted)Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_1Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_2Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted)Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_4Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_5Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_6Top Tier ZHVI (Smoothed) (Seasonally Adjusted)Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_8Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_9
0348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-01-31NaNNaNNaNNaNNaNNaNNaNNaNNaN81310.639504
1348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-02-29NaNNaNNaNNaNNaNNaNNaNNaNNaN80419.761984
2348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-03-31NaNNaNNaNNaNNaNNaNNaNNaNNaN80480.449461
3348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-04-30NaNNaNNaNNaNNaNNaNNaNNaNNaN79799.206525
4348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-05-31NaNNaNNaNNaNNaNNaNNaNNaNNaN79666.469861
.........................................................
1179076251WyomingstatenanAll Bedroomscondo2023-09-30NaNNaNNaNNaNNaN486974.735908NaNNaNNaNNaN
1179086251WyomingstatenanAll Bedroomscondo2023-10-31NaNNaNNaNNaNNaN485847.539614NaNNaNNaNNaN
1179096251WyomingstatenanAll Bedroomscondo2023-11-30NaNNaNNaNNaNNaN484223.885775NaNNaNNaNNaN
1179106251WyomingstatenanAll Bedroomscondo2023-12-31NaNNaNNaNNaNNaN481522.403338NaNNaNNaNNaN
1179116251WyomingstatenanAll Bedroomscondo2024-01-31NaNNaNNaNNaNNaN481181.718200NaNNaNNaNNaN
\n", "

117912 rows × 18 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName Bedroom Count \\\n", "0 3 48 Alaska state nan 1-Bedrooms \n", "1 3 48 Alaska state nan 1-Bedrooms \n", "2 3 48 Alaska state nan 1-Bedrooms \n", "3 3 48 Alaska state nan 1-Bedrooms \n", "4 3 48 Alaska state nan 1-Bedrooms \n", "... ... ... ... ... ... ... \n", "117907 62 51 Wyoming state nan All Bedrooms \n", "117908 62 51 Wyoming state nan All Bedrooms \n", "117909 62 51 Wyoming state nan All Bedrooms \n", "117910 62 51 Wyoming state nan All Bedrooms \n", "117911 62 51 Wyoming state nan All Bedrooms \n", "\n", " Home Type Date \\\n", "0 all homes (SFR/condo) 2000-01-31 \n", "1 all homes (SFR/condo) 2000-02-29 \n", "2 all homes (SFR/condo) 2000-03-31 \n", "3 all homes (SFR/condo) 2000-04-30 \n", "4 all homes (SFR/condo) 2000-05-31 \n", "... ... ... \n", "117907 condo 2023-09-30 \n", "117908 condo 2023-10-31 \n", "117909 condo 2023-11-30 \n", "117910 condo 2023-12-31 \n", "117911 condo 2024-01-31 \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_1 \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_2 \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_4 \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_5 \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 486974.735908 \n", "117908 485847.539614 \n", "117909 484223.885775 \n", "117910 481522.403338 \n", "117911 481181.718200 \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_6 \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Top Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_8 \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted)_9 \n", "0 81310.639504 \n", "1 80419.761984 \n", "2 80480.449461 \n", "3 79799.206525 \n", "4 79666.469861 \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", "[117912 rows x 18 columns]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# base cols RegionID,SizeRank,RegionName,RegionType,StateName\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", " exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Bedroom Count\",\n", " \"Home Type\",\n", " ]\n", "\n", " if \"Zip\" in filename:\n", " continue\n", " if \"Neighborhood\" in filename:\n", " continue\n", " if \"City\" in filename:\n", " continue\n", " if \"Metro\" in filename:\n", " continue\n", " if \"County\" in filename:\n", " continue\n", "\n", " if \"City\" in filename:\n", " exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Bedroom Count\",\n", " \"Home Type\",\n", " # City Specific\n", " \"State\",\n", " \"Metro\",\n", " \"CountyName\",\n", " ]\n", " elif \"Zip\" in filename:\n", " exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Bedroom Count\",\n", " \"Home Type\",\n", " # Zip Specific\n", " \"State\",\n", " \"City\",\n", " \"Metro\",\n", " \"CountyName\",\n", " ]\n", " elif \"County\" in filename:\n", " exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Bedroom Count\",\n", " \"Home Type\",\n", " # County Specific\n", " \"State\",\n", " \"Metro\",\n", " \"StateCodeFIPS\",\n", " \"MunicipalCodeFIPS\",\n", " ]\n", " elif \"Neighborhood\" in filename:\n", " exclude_columns = [\n", " \"RegionID\",\n", " \"SizeRank\",\n", " \"RegionName\",\n", " \"RegionType\",\n", " \"StateName\",\n", " \"Bedroom Count\",\n", " \"Home Type\",\n", " # Neighborhood Specific\n", " \"State\",\n", " \"City\",\n", " \"Metro\",\n", " \"CountyName\",\n", " ]\n", "\n", " if \"_bdrmcnt_1_\" in filename:\n", " cur_df[\"Bedroom Count\"] = \"1-Bedrooms\"\n", " elif \"_bdrmcnt_2_\" in filename:\n", " cur_df[\"Bedroom Count\"] = \"2-Bedrooms\"\n", " elif \"_bdrmcnt_3_\" in filename:\n", " cur_df[\"Bedroom Count\"] = \"3-Bedrooms\"\n", " elif \"_bdrmcnt_4_\" in filename:\n", " cur_df[\"Bedroom Count\"] = \"4 Bedrooms\"\n", " elif \"_bdrmcnt_5_\" in filename:\n", " cur_df[\"Bedroom Count\"] = \"5+ Bedrooms\"\n", " else:\n", " cur_df[\"Bedroom Count\"] = \"All Bedrooms\"\n", "\n", " if \"_uc_sfr_\" in filename:\n", " cur_df[\"Home Type\"] = \"SFR\"\n", " elif \"_uc_sfrcondo_\" in filename:\n", " cur_df[\"Home Type\"] = \"all homes (SFR/condo)\"\n", " elif \"_uc_condo_\" in filename:\n", " cur_df[\"Home Type\"] = \"condo\"\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", " if \"_tier_0.33_0.67_\" in filename:\n", " col_name = \"Mid Tier ZHVI\"\n", " if smoothed:\n", " col_name += \" (Smoothed)\"\n", " if seasonally_adjusted:\n", " col_name += \" (Seasonally Adjusted)\"\n", "\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=col_name,\n", " )\n", " elif \"_tier_0.0_0.33_\" in filename:\n", " col_name = \"Bottom Tier ZHVI\"\n", " if smoothed:\n", " col_name += \" (Smoothed)\"\n", " if seasonally_adjusted:\n", " col_name += \" (Seasonally Adjusted)\"\n", "\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=col_name,\n", " )\n", " elif \"_tier_0.67_1.0_\" in filename:\n", " col_name = \"Top Tier ZHVI\"\n", " if smoothed:\n", " col_name += \" (Smoothed)\"\n", " if seasonally_adjusted:\n", " col_name += \" (Seasonally Adjusted)\"\n", "\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=col_name,\n", " )\n", " else:\n", " col_name = \"ZHVI\"\n", " if smoothed:\n", " col_name += \" (Smoothed)\"\n", " if seasonally_adjusted:\n", " col_name += \" (Seasonally Adjusted)\"\n", "\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=col_name,\n", " )\n", "\n", " cur_df[\"StateName\"] = cur_df[\"StateName\"].astype(str)\n", " cur_df[\"RegionName\"] = cur_df[\"RegionName\"].astype(str)\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", " print(i)\n", " print(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", " \"Bedroom Count\",\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", "combined_df" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ZHVI\n", "Mid Tier ZHVI\n", "Bottom Tier ZHVI\n", "Top Tier ZHVI\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", "
RegionIDSizeRankRegionNameRegionTypeStateNameBedroom CountHome TypeDateMid Tier ZHVI (Smoothed) (Seasonally Adjusted)Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted)Top Tier ZHVI (Smoothed) (Seasonally Adjusted)ZHVIMid Tier ZHVI
0348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-01-31NaNNaNNaN81310.63950481310.639504
1348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-02-29NaNNaNNaN80419.76198480419.761984
2348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-03-31NaNNaNNaN80480.44946180480.449461
3348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-04-30NaNNaNNaN79799.20652579799.206525
4348Alaskastatenan1-Bedroomsall homes (SFR/condo)2000-05-31NaNNaNNaN79666.46986179666.469861
..........................................
1179076251WyomingstatenanAll Bedroomscondo2023-09-30NaNNaNNaN486974.735908486974.735908
1179086251WyomingstatenanAll Bedroomscondo2023-10-31NaNNaNNaN485847.539614485847.539614
1179096251WyomingstatenanAll Bedroomscondo2023-11-30NaNNaNNaN484223.885775484223.885775
1179106251WyomingstatenanAll Bedroomscondo2023-12-31NaNNaNNaN481522.403338481522.403338
1179116251WyomingstatenanAll Bedroomscondo2024-01-31NaNNaNNaN481181.718200481181.718200
\n", "

117912 rows × 13 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName Bedroom Count \\\n", "0 3 48 Alaska state nan 1-Bedrooms \n", "1 3 48 Alaska state nan 1-Bedrooms \n", "2 3 48 Alaska state nan 1-Bedrooms \n", "3 3 48 Alaska state nan 1-Bedrooms \n", "4 3 48 Alaska state nan 1-Bedrooms \n", "... ... ... ... ... ... ... \n", "117907 62 51 Wyoming state nan All Bedrooms \n", "117908 62 51 Wyoming state nan All Bedrooms \n", "117909 62 51 Wyoming state nan All Bedrooms \n", "117910 62 51 Wyoming state nan All Bedrooms \n", "117911 62 51 Wyoming state nan All Bedrooms \n", "\n", " Home Type Date \\\n", "0 all homes (SFR/condo) 2000-01-31 \n", "1 all homes (SFR/condo) 2000-02-29 \n", "2 all homes (SFR/condo) 2000-03-31 \n", "3 all homes (SFR/condo) 2000-04-30 \n", "4 all homes (SFR/condo) 2000-05-31 \n", "... ... ... \n", "117907 condo 2023-09-30 \n", "117908 condo 2023-10-31 \n", "117909 condo 2023-11-30 \n", "117910 condo 2023-12-31 \n", "117911 condo 2024-01-31 \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Top Tier ZHVI (Smoothed) (Seasonally Adjusted) ZHVI \\\n", "0 NaN 81310.639504 \n", "1 NaN 80419.761984 \n", "2 NaN 80480.449461 \n", "3 NaN 79799.206525 \n", "4 NaN 79666.469861 \n", "... ... ... \n", "117907 NaN 486974.735908 \n", "117908 NaN 485847.539614 \n", "117909 NaN 484223.885775 \n", "117910 NaN 481522.403338 \n", "117911 NaN 481181.718200 \n", "\n", " Mid Tier ZHVI \n", "0 81310.639504 \n", "1 80419.761984 \n", "2 80480.449461 \n", "3 79799.206525 \n", "4 79666.469861 \n", "... ... \n", "117907 486974.735908 \n", "117908 485847.539614 \n", "117909 484223.885775 \n", "117910 481522.403338 \n", "117911 481181.718200 \n", "\n", "[117912 rows x 13 columns]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# iterate over rows of combined df and coalesce column values across columns that start with \"Median Sale Price\"\n", "columns_to_coalesce = [\"ZHVI\", \"Mid Tier ZHVI\", \"Bottom Tier ZHVI\", \"Top Tier ZHVI\"]\n", "\n", "for column_to_coalesce in columns_to_coalesce:\n", " print(column_to_coalesce)\n", " for index, row in combined_df.iterrows():\n", " for col in combined_df.columns:\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", "\n", "combined_df" ] }, { "cell_type": "code", "execution_count": 21, "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", "
RegionIDSizeRankRegionNameRegionTypeStateNameBedroom CountHome TypeDateMid Tier ZHVI (Smoothed) (Seasonally Adjusted)Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted)Top Tier ZHVI (Smoothed) (Seasonally Adjusted)ZHVIMid Tier ZHVI
0348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-01-31NaNNaNNaN81310.63950481310.639504
1348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-02-29NaNNaNNaN80419.76198480419.761984
2348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-03-31NaNNaNNaN80480.44946180480.449461
3348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-04-30NaNNaNNaN79799.20652579799.206525
4348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-05-31NaNNaNNaN79666.46986179666.469861
..........................................
1179076251WyomingstateWyomingAll Bedroomscondo2023-09-30NaNNaNNaN486974.735908486974.735908
1179086251WyomingstateWyomingAll Bedroomscondo2023-10-31NaNNaNNaN485847.539614485847.539614
1179096251WyomingstateWyomingAll Bedroomscondo2023-11-30NaNNaNNaN484223.885775484223.885775
1179106251WyomingstateWyomingAll Bedroomscondo2023-12-31NaNNaNNaN481522.403338481522.403338
1179116251WyomingstateWyomingAll Bedroomscondo2024-01-31NaNNaNNaN481181.718200481181.718200
\n", "

117912 rows × 13 columns

\n", "
" ], "text/plain": [ " RegionID SizeRank RegionName RegionType StateName Bedroom Count \\\n", "0 3 48 Alaska state Alaska 1-Bedrooms \n", "1 3 48 Alaska state Alaska 1-Bedrooms \n", "2 3 48 Alaska state Alaska 1-Bedrooms \n", "3 3 48 Alaska state Alaska 1-Bedrooms \n", "4 3 48 Alaska state Alaska 1-Bedrooms \n", "... ... ... ... ... ... ... \n", "117907 62 51 Wyoming state Wyoming All Bedrooms \n", "117908 62 51 Wyoming state Wyoming All Bedrooms \n", "117909 62 51 Wyoming state Wyoming All Bedrooms \n", "117910 62 51 Wyoming state Wyoming All Bedrooms \n", "117911 62 51 Wyoming state Wyoming All Bedrooms \n", "\n", " Home Type Date \\\n", "0 all homes (SFR/condo) 2000-01-31 \n", "1 all homes (SFR/condo) 2000-02-29 \n", "2 all homes (SFR/condo) 2000-03-31 \n", "3 all homes (SFR/condo) 2000-04-30 \n", "4 all homes (SFR/condo) 2000-05-31 \n", "... ... ... \n", "117907 condo 2023-09-30 \n", "117908 condo 2023-10-31 \n", "117909 condo 2023-11-30 \n", "117910 condo 2023-12-31 \n", "117911 condo 2024-01-31 \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Top Tier ZHVI (Smoothed) (Seasonally Adjusted) ZHVI \\\n", "0 NaN 81310.639504 \n", "1 NaN 80419.761984 \n", "2 NaN 80480.449461 \n", "3 NaN 79799.206525 \n", "4 NaN 79666.469861 \n", "... ... ... \n", "117907 NaN 486974.735908 \n", "117908 NaN 485847.539614 \n", "117909 NaN 484223.885775 \n", "117910 NaN 481522.403338 \n", "117911 NaN 481181.718200 \n", "\n", " Mid Tier ZHVI \n", "0 81310.639504 \n", "1 80419.761984 \n", "2 80480.449461 \n", "3 79799.206525 \n", "4 79666.469861 \n", "... ... \n", "117907 486974.735908 \n", "117908 485847.539614 \n", "117909 484223.885775 \n", "117910 481522.403338 \n", "117911 481181.718200 \n", "\n", "[117912 rows x 13 columns]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_df = combined_df\n", "\n", "for index, row in final_df.iterrows():\n", " if row[\"RegionType\"] == \"city\":\n", " final_df.at[index, \"City\"] = row[\"RegionName\"]\n", " elif row[\"RegionType\"] == \"county\":\n", " final_df.at[index, \"County\"] = row[\"RegionName\"]\n", " if row[\"RegionType\"] == \"state\":\n", " final_df.at[index, \"StateName\"] = row[\"RegionName\"]\n", "\n", "# coalesce State and StateName columns\n", "# final_df[\"State\"] = final_df[\"State\"].combine_first(final_df[\"StateName\"])\n", "# final_df[\"County\"] = final_df[\"County\"].combine_first(final_df[\"CountyName\"])\n", "\n", "# final_df = final_df.drop(\n", "# columns=[\n", "# \"StateName\",\n", "# # \"CountyName\"\n", "# ]\n", "# )\n", "final_df" ] }, { "cell_type": "code", "execution_count": 22, "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 TypeStateBedroom CountHome TypeDateMid Tier ZHVI (Smoothed) (Seasonally Adjusted)Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted)Top Tier ZHVI (Smoothed) (Seasonally Adjusted)ZHVIMid Tier ZHVI
0348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-01-31NaNNaNNaN81310.63950481310.639504
1348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-02-29NaNNaNNaN80419.76198480419.761984
2348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-03-31NaNNaNNaN80480.44946180480.449461
3348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-04-30NaNNaNNaN79799.20652579799.206525
4348AlaskastateAlaska1-Bedroomsall homes (SFR/condo)2000-05-31NaNNaNNaN79666.46986179666.469861
..........................................
1179076251WyomingstateWyomingAll Bedroomscondo2023-09-30NaNNaNNaN486974.735908486974.735908
1179086251WyomingstateWyomingAll Bedroomscondo2023-10-31NaNNaNNaN485847.539614485847.539614
1179096251WyomingstateWyomingAll Bedroomscondo2023-11-30NaNNaNNaN484223.885775484223.885775
1179106251WyomingstateWyomingAll Bedroomscondo2023-12-31NaNNaNNaN481522.403338481522.403338
1179116251WyomingstateWyomingAll Bedroomscondo2024-01-31NaNNaNNaN481181.718200481181.718200
\n", "

117912 rows × 13 columns

\n", "
" ], "text/plain": [ " Region ID Size Rank Region Region Type State Bedroom Count \\\n", "0 3 48 Alaska state Alaska 1-Bedrooms \n", "1 3 48 Alaska state Alaska 1-Bedrooms \n", "2 3 48 Alaska state Alaska 1-Bedrooms \n", "3 3 48 Alaska state Alaska 1-Bedrooms \n", "4 3 48 Alaska state Alaska 1-Bedrooms \n", "... ... ... ... ... ... ... \n", "117907 62 51 Wyoming state Wyoming All Bedrooms \n", "117908 62 51 Wyoming state Wyoming All Bedrooms \n", "117909 62 51 Wyoming state Wyoming All Bedrooms \n", "117910 62 51 Wyoming state Wyoming All Bedrooms \n", "117911 62 51 Wyoming state Wyoming All Bedrooms \n", "\n", " Home Type Date \\\n", "0 all homes (SFR/condo) 2000-01-31 \n", "1 all homes (SFR/condo) 2000-02-29 \n", "2 all homes (SFR/condo) 2000-03-31 \n", "3 all homes (SFR/condo) 2000-04-30 \n", "4 all homes (SFR/condo) 2000-05-31 \n", "... ... ... \n", "117907 condo 2023-09-30 \n", "117908 condo 2023-10-31 \n", "117909 condo 2023-11-30 \n", "117910 condo 2023-12-31 \n", "117911 condo 2024-01-31 \n", "\n", " Mid Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Bottom Tier ZHVI (Smoothed) (Seasonally Adjusted) \\\n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 NaN \n", "4 NaN \n", "... ... \n", "117907 NaN \n", "117908 NaN \n", "117909 NaN \n", "117910 NaN \n", "117911 NaN \n", "\n", " Top Tier ZHVI (Smoothed) (Seasonally Adjusted) ZHVI \\\n", "0 NaN 81310.639504 \n", "1 NaN 80419.761984 \n", "2 NaN 80480.449461 \n", "3 NaN 79799.206525 \n", "4 NaN 79666.469861 \n", "... ... ... \n", "117907 NaN 486974.735908 \n", "117908 NaN 485847.539614 \n", "117909 NaN 484223.885775 \n", "117910 NaN 481522.403338 \n", "117911 NaN 481181.718200 \n", "\n", " Mid Tier ZHVI \n", "0 81310.639504 \n", "1 80419.761984 \n", "2 80480.449461 \n", "3 79799.206525 \n", "4 79666.469861 \n", "... ... \n", "117907 486974.735908 \n", "117908 485847.539614 \n", "117909 484223.885775 \n", "117910 481522.403338 \n", "117911 481181.718200 \n", "\n", "[117912 rows x 13 columns]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_df = final_df.rename(\n", " columns={\n", " \"RegionID\": \"Region ID\",\n", " \"SizeRank\": \"Size Rank\",\n", " \"RegionName\": \"Region\",\n", " \"RegionType\": \"Region Type\",\n", " \"StateCodeFIPS\": \"State Code FIPS\",\n", " \"StateName\": \"State\",\n", " \"MunicipalCodeFIPS\": \"Municipal Code FIPS\",\n", " }\n", ")\n", "\n", "final_df" ] }, { "cell_type": "code", "execution_count": 23, "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 }