usa_opioid_overdose / 02_Codes /02_population_script.py
revanth7667's picture
updated population and created mortality files
6a0acc8
raw
history blame
3.45 kB
# importing pandas and setting default option
import pandas as pd
pd.set_option("mode.copy_on_write", True)
# reading the raw files
df = pd.read_csv(".01_Data/01_Raw/raw_population.txt", sep="\t")
fips = pd.read_csv(".01_Data/01_Raw/county_fips.csv")
abbreviations = pd.read_csv(".01_Data/01_Raw/state_abbreviations.csv")
# ------------------------------------------
# dropping the unnecessary columns
df1 = df.drop(columns=["Notes"])
# Dropping unnecessary rows
# 1. removing the rows with na values generated due to Notes, using state column for reference
df1 = df1.dropna(subset=["State"])
# 2. Removing Alaska
df1 = df1[df1["State"] != "Alaska"]
# ------------------------------------------
# Correcting Data Types for columns
df2 = df1.copy()
# 1. Saving state code as padded string
df2["State Code"] = df2["State Code"].astype(int).astype(str).str.zfill(2)
# 2. Saving county code as padded string
df2["County Code"] = df2["County Code"].astype(int).astype(str).str.zfill(5)
# padding fips to have consistency
fips["countyfips"] = fips["countyfips"].astype(str).str.zfill(5)
# 3. Converting Year to Integer
df2["Yearly July 1st Estimates"] = df2["Yearly July 1st Estimates"].astype(int)
# 4. Converting Population to Integer
# replacing the missing values with 0 for now <-------------------Change this later if required
df2["Population"] = df2["Population"].replace("Missing", 0)
df2["Population"] = df2["Population"].astype(int)
# ------------------------------------------
# creating subset of data for analysis
df3 = df2.copy()
# rename columns
df3 = df3.rename(
columns={
"Yearly July 1st Estimates": "Year",
"State Code": "State_Code",
"County Code": "County_Code",
}
)
# reorder columns
df3 = df3[
[
"State",
"State_Code",
"County",
"County_Code",
"Year",
"Population",
]
]
# ------------------------------------------
# mapping with fips for proper county names
df4 = pd.merge(
df3,
fips,
how="left",
left_on="County_Code",
right_on="countyfips",
validate="m:1",
indicator=True,
)
# ------------------------------------------
# correcting the county names where fips mapping failed
df4.loc[df4["County"] == "Montgomery County, AR", "BUYER_COUNTY"] = "MONTGOMERY"
df4.loc[df4["County"] == "Kalawao County, HI", "BUYER_COUNTY"] = "KALAWAO"
df4.loc[df4["County"] == "Oglala Lakota County, SD", "BUYER_COUNTY"] = "OGLALA LAKOTA"
# ------------------------------------------
# creating final dataframe
# ------------------------------------------
# mapping state names to abbreviations
# rename abbreviations columns to match with the main dataframe
abbreviations = abbreviations.rename(
columns={
"state": "State",
"code": "State_Code",
}
)
# merge the dataframes
df5 = pd.merge(
df4[["State", "BUYER_COUNTY", "County_Code", "Year", "Population"]],
abbreviations[["State", "State_Code"]],
how="left",
left_on="State",
right_on="State",
validate="m:1",
)
# rename columns
df5 = df5.rename(
columns={
"BUYER_COUNTY": "County",
}
)
# reorder columns
df5 = df5[
[
"State",
"State_Code",
"County",
"County_Code",
"Year",
"Population",
]
]
# ------------------------------------------
# Writing to Parquet
df5.to_parquet(".01_Data/02_processed/01_Population.parquet", index=False)