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

# In[1]:


import pandas as pd
import os

from helpers import get_combined_df, save_final_df_as_jsonl


# In[2]:


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


# In[3]:


data_frames = []

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))

        cols = ["Month Over Month %", "Quarter Over Quarter %", "Year Over Year %"]
        if filename.endswith("sm_sa_month.csv"):
            # print('Smoothed')
            cur_df.columns = list(cur_df.columns[:-3]) + [
                x + " (Smoothed) (Seasonally Adjusted)" for x in cols
            ]
        else:
            # print('Raw')
            cur_df.columns = list(cur_df.columns[:-3]) + cols
        cur_df["RegionName"] = cur_df["RegionName"].astype(str)

        data_frames.append(cur_df)


combined_df = get_combined_df(
    data_frames,
    [
        "RegionID",
        "RegionType",
        "SizeRank",
        "StateName",
        "BaseDate",
    ],
)

combined_df


# In[4]:


# Adjust columns
final_df = combined_df
final_df = combined_df.drop("StateName", axis=1)
final_df = final_df.rename(
    columns={
        "CountyName": "County",
        "BaseDate": "Date",
        "RegionName": "Region",
        "RegionID": "Region ID",
        "SizeRank": "Size Rank",
    }
)

# iterate over rows of final_df and populate State and City columns if the regionType is msa
for index, row in final_df.iterrows():
    if row["RegionType"] == "msa":
        regionName = row["Region"]
        # final_df.at[index, 'Metro'] = regionName

        city = regionName.split(", ")[0]
        final_df.at[index, "City"] = city

        state = regionName.split(", ")[1]
        final_df.at[index, "State"] = state

final_df


# In[9]:


save_final_df_as_jsonl(FULL_PROCESSED_DIR_PATH, final_df)