File size: 4,169 Bytes
f2ba714
 
 
 
 
 
 
 
 
 
fc88e22
f2ba714
 
 
 
 
 
 
 
 
 
fc88e22
f2ba714
 
fc88e22
f2ba714
 
 
 
 
 
 
 
 
 
 
fc88e22
 
 
f2ba714
 
fc88e22
f2ba714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc88e22
f2ba714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc88e22
f2ba714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc88e22
f2ba714
 
fc88e22
f2ba714
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import pandas as pd
import os

from helpers import (
    get_data_path_for_config,
    get_combined_df,
    save_final_df_as_jsonl,
    handle_slug_column_mappings,
    set_home_type,
)


# In[2]:


CONFIG_NAME = "home_values"


# In[3]:


data_frames = []

slug_column_mappings = {
    "_tier_0.0_0.33_": "Bottom Tier ZHVI",
    "_tier_0.33_0.67_": "Mid Tier ZHVI",
    "_tier_0.67_1.0_": "Top Tier ZHVI",
    "": "ZHVI",
}

data_dir_path = get_data_path_for_config(CONFIG_NAME)

for filename in os.listdir(data_dir_path):
    if filename.endswith(".csv"):
        print("processing " + filename)
        cur_df = pd.read_csv(os.path.join(data_dir_path, filename))
        exclude_columns = [
            "RegionID",
            "SizeRank",
            "RegionName",
            "RegionType",
            "StateName",
            "Bedroom Count",
            "Home Type",
        ]

        if "Zip" in filename:
            continue
        if "Neighborhood" in filename:
            continue
        if "City" in filename:
            continue
        if "Metro" in filename:
            continue
        if "County" in filename:
            continue

        if "City" in filename:
            exclude_columns = exclude_columns + ["State", "Metro", "CountyName"]
        elif "Zip" in filename:
            exclude_columns = exclude_columns + [
                "State",
                "City",
                "Metro",
                "CountyName",
            ]
        elif "County" in filename:
            exclude_columns = exclude_columns + [
                "State",
                "Metro",
                "StateCodeFIPS",
                "MunicipalCodeFIPS",
            ]
        elif "Neighborhood" in filename:
            exclude_columns = exclude_columns + [
                "State",
                "City",
                "Metro",
                "CountyName",
            ]

        if "_bdrmcnt_1_" in filename:
            cur_df["Bedroom Count"] = "1-Bedroom"
        elif "_bdrmcnt_2_" in filename:
            cur_df["Bedroom Count"] = "2-Bedrooms"
        elif "_bdrmcnt_3_" in filename:
            cur_df["Bedroom Count"] = "3-Bedrooms"
        elif "_bdrmcnt_4_" in filename:
            cur_df["Bedroom Count"] = "4-Bedrooms"
        elif "_bdrmcnt_5_" in filename:
            cur_df["Bedroom Count"] = "5+-Bedrooms"
        else:
            cur_df["Bedroom Count"] = "All Bedrooms"

        cur_df = set_home_type(cur_df, filename)

        cur_df["StateName"] = cur_df["StateName"].astype(str)
        cur_df["RegionName"] = cur_df["RegionName"].astype(str)

        data_frames = handle_slug_column_mappings(
            data_frames, slug_column_mappings, exclude_columns, filename, cur_df
        )


combined_df = get_combined_df(
    data_frames,
    [
        "RegionID",
        "SizeRank",
        "RegionName",
        "RegionType",
        "StateName",
        "Bedroom Count",
        "Home Type",
        "Date",
    ],
)

combined_df


# In[4]:


final_df = combined_df

for index, row in final_df.iterrows():
    if row["RegionType"] == "city":
        final_df.at[index, "City"] = row["RegionName"]
    elif row["RegionType"] == "county":
        final_df.at[index, "County"] = row["RegionName"]
    if row["RegionType"] == "state":
        final_df.at[index, "StateName"] = row["RegionName"]

# coalesce State and StateName columns
# final_df["State"] = final_df["State"].combine_first(final_df["StateName"])
# final_df["County"] = final_df["County"].combine_first(final_df["CountyName"])

# final_df = final_df.drop(
#     columns=[
#         "StateName",
#         # "CountyName"
#     ]
# )
final_df


# In[5]:


final_df = final_df.rename(
    columns={
        "RegionID": "Region ID",
        "SizeRank": "Size Rank",
        "RegionName": "Region",
        "RegionType": "Region Type",
        "StateCodeFIPS": "State Code FIPS",
        "StateName": "State",
        "MunicipalCodeFIPS": "Municipal Code FIPS",
    }
)

final_df["Date"] = pd.to_datetime(final_df["Date"], format="%Y-%m-%d")

final_df


# In[6]:


save_final_df_as_jsonl(CONFIG_NAME, final_df)