|
|
|
import pandas as pd |
|
|
|
pd.set_option("mode.copy_on_write", True) |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
df1 = df.drop(columns=["Notes"]) |
|
|
|
|
|
|
|
df1 = df1.dropna(subset=["State"]) |
|
|
|
|
|
df1 = df1[df1["State"] != "Alaska"] |
|
|
|
|
|
|
|
|
|
df2 = df1.copy() |
|
|
|
|
|
df2["State Code"] = df2["State Code"].astype(int).astype(str).str.zfill(2) |
|
|
|
|
|
df2["County Code"] = df2["County Code"].astype(int).astype(str).str.zfill(5) |
|
|
|
|
|
fips["countyfips"] = fips["countyfips"].astype(str).str.zfill(5) |
|
|
|
|
|
df2["Yearly July 1st Estimates"] = df2["Yearly July 1st Estimates"].astype(int) |
|
|
|
|
|
|
|
df2["Population"] = df2["Population"].replace("Missing", 0) |
|
df2["Population"] = df2["Population"].astype(int) |
|
|
|
|
|
|
|
|
|
df3 = df2.copy() |
|
|
|
|
|
df3 = df3.rename( |
|
columns={ |
|
"Yearly July 1st Estimates": "Year", |
|
"State Code": "State_Code", |
|
"County Code": "County_Code", |
|
} |
|
) |
|
|
|
|
|
df3 = df3[ |
|
[ |
|
"State", |
|
"State_Code", |
|
"County", |
|
"County_Code", |
|
"Year", |
|
"Population", |
|
] |
|
] |
|
|
|
|
|
|
|
|
|
df4 = pd.merge( |
|
df3, |
|
fips, |
|
how="left", |
|
left_on="County_Code", |
|
right_on="countyfips", |
|
validate="m:1", |
|
indicator=True, |
|
) |
|
|
|
|
|
|
|
|
|
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" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abbreviations = abbreviations.rename( |
|
columns={ |
|
"state": "State", |
|
"code": "State_Code", |
|
} |
|
) |
|
|
|
|
|
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", |
|
) |
|
|
|
|
|
|
|
df5 = df5.rename( |
|
columns={ |
|
"BUYER_COUNTY": "County", |
|
} |
|
) |
|
|
|
|
|
df5 = df5[ |
|
[ |
|
"State", |
|
"State_Code", |
|
"County", |
|
"County_Code", |
|
"Year", |
|
"Population", |
|
] |
|
] |
|
|
|
|
|
|
|
|
|
df5.to_parquet(".01_Data/02_processed/01_Population.parquet", index=False) |
|
|