File size: 2,038 Bytes
c83a125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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 = "sales"


# In[3]:


data_frames = []

exclude_columns = [
    "RegionID",
    "SizeRank",
    "RegionName",
    "RegionType",
    "StateName",
    "Home Type",
]

slug_column_mappings = {
    "_median_sale_to_list_": "Median Sale to List Ratio",
    "_mean_sale_to_list_": "Mean Sale to List Ratio",
    "_median_sale_price_": "Median Sale Price",
    "_pct_sold_above_list_": "% Sold Above List",
    "_pct_sold_below_list_": "% Sold Below List",
    "_sales_count_now_": "Nowcast",
}

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)
        # ignore monthly data for now since it is redundant
        if "month" in filename:
            continue

        cur_df = pd.read_csv(os.path.join(data_dir_path, filename))

        cur_df = set_home_type(cur_df, filename)

        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[4]:


# Adjust column names
final_df = combined_df.rename(
    columns={
        "RegionID": "Region ID",
        "SizeRank": "Size Rank",
        "RegionName": "Region",
        "RegionType": "Region Type",
        "StateName": "State",
    }
)

final_df["Date"] = pd.to_datetime(final_df["Date"])
final_df.sort_values(by=["Region ID", "Home Type", "Date"])


# In[5]:


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)