File size: 3,833 Bytes
69c22e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# coding: utf-8

# In[2]:


import pandas as pd
import os

from helpers import (
    get_combined_df,
    save_final_df_as_jsonl,
    handle_slug_column_mappings,
)


# In[3]:


DATA_DIR = "../data"
PROCESSED_DIR = "../processed/"
FACET_DIR = "rentals/"
FULL_DATA_DIR_PATH = os.path.join(DATA_DIR, FACET_DIR)
FULL_PROCESSED_DIR_PATH = os.path.join(PROCESSED_DIR, FACET_DIR)


# In[7]:


data_frames = []

slug_column_mappings = {"": "Rent"}

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

        if "_sfrcondomfr_" in filename:
            cur_df["Home Type"] = "all homes plus multifamily"
            # change column type to string
            cur_df["RegionName"] = cur_df["RegionName"].astype(str)
            if "City" in filename:
                exclude_columns = [
                    "RegionID",
                    "SizeRank",
                    "RegionName",
                    "RegionType",
                    "StateName",
                    "Home Type",
                    # City Specific
                    "State",
                    "Metro",
                    "CountyName",
                ]
            elif "Zip" in filename:
                exclude_columns = [
                    "RegionID",
                    "SizeRank",
                    "RegionName",
                    "RegionType",
                    "StateName",
                    "Home Type",
                    # Zip Specific
                    "State",
                    "City",
                    "Metro",
                    "CountyName",
                ]
            elif "County" in filename:
                exclude_columns = [
                    "RegionID",
                    "SizeRank",
                    "RegionName",
                    "RegionType",
                    "StateName",
                    "Home Type",
                    # County Specific
                    "State",
                    "Metro",
                    "StateCodeFIPS",
                    "MunicipalCodeFIPS",
                ]

        elif "_sfr_" in filename:
            cur_df["Home Type"] = "SFR"
        elif "_mfr_" in filename:
            cur_df["Home Type"] = "multifamily"

        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",
        "Home Type",
        "Date",
    ],
)

combined_df


# In[8]:


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"]

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

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


# In[6]:


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

final_df


# In[7]:


save_final_df_as_jsonl(FULL_PROCESSED_DIR_PATH, final_df)