File size: 4,671 Bytes
6e55cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
To impute the missing death data based on the state level mortality.

Saves final dataset as data.parquet

refer to the 05_master_eda.ipynb for EDA and other details like how the data was imputed.
"""

# importing libraries and setting default option
import pandas as pd
import numpy as np

pd.set_option("mode.copy_on_write", True)

# reading the data files
df = pd.read_parquet(".01_Data/02_Processed/02_Mortality.parquet")
population = pd.read_parquet(".01_Data/02_Processed/01_Population.parquet")

# ------------------------------------------
# initial Cleaning
df = df[df["State"] != "AK"]  # dropping ALASKA since it is Out of Scope (OOS)

# We will consider only unintentional drug related deaths since other have very few values
df = df[df["Cause"] == "Drug poisonings (overdose) Unintentional (X40-X44)"]

df = (
    df.dropna()
)  # dropping rows with missing values since they are very few and will be imputed later

# ------------------------------------------
# Merge with population data
combined = pd.merge(
    df,
    population,
    on=["State", "State_Code", "County", "County_Code", "Year"],
    how="left",
    validate="1:1",
    indicator=True,
)

# ------------------------------------------
# Clean the Merged Data
df2 = combined[
    [
        "State",
        "State_Code",
        "County",
        "County_Code",
        "Year",
        "Population",
        "Deaths",
    ]
]

# ------------------------------------------
# calculating Mortality Rate (County Level)
df3 = df2.copy()
df3["Mortality_Rate"] = df3["Deaths"] / df3["Population"]

# ------------------------------------------
# Mortality Rate (State Level)

# aggregate at state-cause level
df4 = (
    df3.groupby(["State", "Year"])
    .agg({"Deaths": "sum", "Population": "sum"})
    .reset_index()
)

# clacualting mortality rate
df4["State_Mortality_Rate"] = df4["Deaths"] / df4["Population"]

# ------------------------------------------
# Creating a list of State-Counties from the POPULATION dataset
st_county = population[
    ["State", "State_Code", "County", "County_Code", "Year"]
].drop_duplicates()

# ------------------------------------------
# Merging State Mortality Rate with State-County list
master = pd.merge(st_county, df4, on=["State", "Year"], how="left", indicator=True)

# dropping NA rows since we have no state level data for them
master = master[master["_merge"] == "both"]

# Cleaning the merged data
master_2 = master[
    [
        "State",
        "State_Code",
        "County",
        "County_Code",
        "Year",
        "State_Mortality_Rate",
    ]
]

# ------------------------------------------
# merge with the county level data
df5 = pd.merge(
    master_2,
    df3,
    on=["State", "State_Code", "County", "County_Code", "Year"],
    how="left",
    indicator=True,
    validate="1:1",
)

# adding a new flag to identify if original data or not
df5["Original"] = df5["_merge"] == "both"

# ------------------------------------------
# Remap with population data to get county population
df6 = pd.merge(
    df5,
    population[["County_Code", "Year", "Population"]],
    on=["County_Code", "Year"],
    how="left",
    validate="1:1",
    indicator="merge2",
)


# ------------------------------------------
def new_death(row):
    """Function to Calcuate the deaths in county using the State Mortality Rate and County Population
    if the deaths are missing in the original dataset.
    Max value is limited to 9 since we know that it can't be 10 or more"""

    if pd.isna(row["Deaths"]):
        return min(int(row["Population_y"] * row["State_Mortality_Rate"]), 9)
    else:
        return row["Deaths"]


# ------------------------------------------
# calautating the Final Deaths by using the new_death function
df7 = df6.copy()
df7["Deaths_2"] = df7.apply(new_death, axis=1)

# ------------------------------------------
# Cleaning the dataset
df8 = df7[
    [
        "State",
        "State_Code",
        "County",
        "County_Code",
        "Year",
        "Population_y",
        "Deaths_2",
        "Original",
        "State_Mortality_Rate",
    ]
]

# Renaming columns
df8 = df8.rename(columns={"Population_y": "Population", "Deaths_2": "Deaths"})

# ------------------------------------------
# Calculating Mortality Rate for each county, if population is 0 then mortality rate is 0
df8["County_Mortality_Rate"] = np.where(
    df8["Population"] == 0, 0, df8["Deaths"] / df8["Population"]
)

# sorting the rows
df9 = df8.sort_values(by=["State", "County", "Year"]).reset_index(drop=True)


# ------------------------------------------
# Saving the Final Dataset
df9.to_parquet("data.parquet", index=False)