|
""" |
|
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. |
|
""" |
|
|
|
|
|
import pandas as pd |
|
import numpy as np |
|
|
|
pd.set_option("mode.copy_on_write", True) |
|
|
|
|
|
df = pd.read_parquet(".01_Data/02_Processed/02_Mortality.parquet") |
|
population = pd.read_parquet(".01_Data/02_Processed/01_Population.parquet") |
|
|
|
|
|
|
|
df = df[df["State"] != "AK"] |
|
|
|
|
|
df = df[df["Cause"] == "Drug poisonings (overdose) Unintentional (X40-X44)"] |
|
|
|
df = ( |
|
df.dropna() |
|
) |
|
|
|
|
|
|
|
combined = pd.merge( |
|
df, |
|
population, |
|
on=["State", "State_Code", "County", "County_Code", "Year"], |
|
how="left", |
|
validate="1:1", |
|
indicator=True, |
|
) |
|
|
|
|
|
|
|
df2 = combined[ |
|
[ |
|
"State", |
|
"State_Code", |
|
"County", |
|
"County_Code", |
|
"Year", |
|
"Population", |
|
"Deaths", |
|
] |
|
] |
|
|
|
|
|
|
|
df3 = df2.copy() |
|
df3["Mortality_Rate"] = df3["Deaths"] / df3["Population"] |
|
|
|
|
|
|
|
|
|
|
|
df4 = ( |
|
df3.groupby(["State", "Year"]) |
|
.agg({"Deaths": "sum", "Population": "sum"}) |
|
.reset_index() |
|
) |
|
|
|
|
|
df4["State_Mortality_Rate"] = df4["Deaths"] / df4["Population"] |
|
|
|
|
|
|
|
st_county = population[ |
|
["State", "State_Code", "County", "County_Code", "Year"] |
|
].drop_duplicates() |
|
|
|
|
|
|
|
master = pd.merge(st_county, df4, on=["State", "Year"], how="left", indicator=True) |
|
|
|
|
|
master = master[master["_merge"] == "both"] |
|
|
|
|
|
master_2 = master[ |
|
[ |
|
"State", |
|
"State_Code", |
|
"County", |
|
"County_Code", |
|
"Year", |
|
"State_Mortality_Rate", |
|
] |
|
] |
|
|
|
|
|
|
|
df5 = pd.merge( |
|
master_2, |
|
df3, |
|
on=["State", "State_Code", "County", "County_Code", "Year"], |
|
how="left", |
|
indicator=True, |
|
validate="1:1", |
|
) |
|
|
|
|
|
df5["Original"] = df5["_merge"] == "both" |
|
|
|
|
|
|
|
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"] |
|
|
|
|
|
|
|
|
|
df7 = df6.copy() |
|
df7["Deaths_2"] = df7.apply(new_death, axis=1) |
|
|
|
|
|
|
|
df8 = df7[ |
|
[ |
|
"State", |
|
"State_Code", |
|
"County", |
|
"County_Code", |
|
"Year", |
|
"Population_y", |
|
"Deaths_2", |
|
"Original", |
|
"State_Mortality_Rate", |
|
] |
|
] |
|
|
|
|
|
df8 = df8.rename(columns={"Population_y": "Population", "Deaths_2": "Deaths"}) |
|
|
|
|
|
|
|
df8["County_Mortality_Rate"] = np.where( |
|
df8["Population"] == 0, 0, df8["Deaths"] / df8["Population"] |
|
) |
|
|
|
|
|
df9 = df8.sort_values(by=["State", "County", "Year"]).reset_index(drop=True) |
|
|
|
|
|
|
|
|
|
df9.to_parquet("data.parquet", index=False) |
|
|