-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from BasisResearch/nl-add-industry
adding industry and urbanization datasets
- Loading branch information
Showing
16 changed files
with
133,056 additions
and
7,584 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from cities.utils.cleaning_utils import standardize_and_scale | ||
from cities.utils.data_grabber import DataGrabber | ||
|
||
|
||
def clean_industry(): | ||
data = DataGrabber() | ||
data.get_features_wide(["gdp"]) | ||
gdp = data.wide["gdp"] | ||
|
||
industry = pd.read_csv("../data/raw/ACSDP5Y2021_DP03_industry.csv") | ||
|
||
assert industry["GEO_ID"].isna() == 0 | ||
|
||
industry["GEO_ID"] = industry["GEO_ID"].str.split("US").str[1] | ||
industry["GEO_ID"] = industry["GEO_ID"].astype("int64") | ||
industry = industry.rename(columns={"GEO_ID": "GeoFIPS"}) | ||
|
||
common_fips = np.intersect1d(gdp["GeoFIPS"].unique(), industry["GeoFIPS"].unique()) | ||
|
||
industry = industry[industry["GeoFIPS"].isin(common_fips)] | ||
|
||
industry = industry.merge(gdp[["GeoFIPS", "GeoName"]], on="GeoFIPS", how="left") | ||
|
||
industry = industry[ | ||
[ | ||
"GeoFIPS", | ||
"GeoName", | ||
"DP03_0004E", | ||
"DP03_0033E", | ||
"DP03_0034E", | ||
"DP03_0035E", | ||
"DP03_0036E", | ||
"DP03_0037E", | ||
"DP03_0038E", | ||
"DP03_0039E", | ||
"DP03_0040E", | ||
"DP03_0041E", | ||
"DP03_0042E", | ||
"DP03_0043E", | ||
"DP03_0044E", | ||
"DP03_0045E", | ||
] | ||
] | ||
|
||
column_name_mapping = { | ||
"DP03_0004E": "employed_sum", | ||
"DP03_0033E": "agri_forestry_mining", | ||
"DP03_0034E": "construction", | ||
"DP03_0035E": "manufacturing", | ||
"DP03_0036E": "wholesale_trade", | ||
"DP03_0037E": "retail_trade", | ||
"DP03_0038E": "transport_utilities", | ||
"DP03_0039E": "information", | ||
"DP03_0040E": "finance_real_estate", | ||
"DP03_0041E": "prof_sci_mgmt_admin", | ||
"DP03_0042E": "education_health", | ||
"DP03_0043E": "arts_entertainment", | ||
"DP03_0044E": "other_services", | ||
"DP03_0045E": "public_admin", | ||
} | ||
|
||
industry.rename(columns=column_name_mapping, inplace=True) | ||
|
||
industry = industry.sort_values(by=["GeoFIPS", "GeoName"]) | ||
|
||
industry.to_csv("../data/raw/industry_absolute.csv", index=False) | ||
|
||
row_sums = industry.iloc[:, 3:].sum(axis=1) | ||
|
||
industry.iloc[:, 3:] = industry.iloc[:, 3:].div(row_sums, axis=0) | ||
industry = industry.drop(["employed_sum"], axis=1) | ||
|
||
industry_wide = industry.copy() | ||
|
||
industry_long = pd.melt( | ||
industry, | ||
id_vars=["GeoFIPS", "GeoName"], | ||
var_name="Category", | ||
value_name="Value", | ||
) | ||
|
||
industry_std_wide = standardize_and_scale(industry) | ||
|
||
industry_std_long = pd.melt( | ||
industry_std_wide.copy(), | ||
id_vars=["GeoFIPS", "GeoName"], | ||
var_name="Category", | ||
value_name="Value", | ||
) | ||
|
||
industry_wide.to_csv("../data/processed/industry_wide.csv", index=False) | ||
industry_long.to_csv("../data/processed/industry_long.csv", index=False) | ||
industry_std_wide.to_csv("../data/processed/industry_std_wide.csv", index=False) | ||
industry_std_long.to_csv("../data/processed/industry_std_long.csv", index=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from cities.utils.cleaning_utils import standardize_and_scale | ||
from cities.utils.data_grabber import DataGrabber | ||
|
||
|
||
def clean_urbanization(): | ||
data = DataGrabber() | ||
data.get_features_wide(["gdp"]) | ||
gdp = data.wide["gdp"] | ||
|
||
dtype_mapping = {"STATE": str, "COUNTY": str} | ||
urbanization = pd.read_csv("../data/raw/2020_UA_COUNTY.csv", dtype=dtype_mapping) | ||
|
||
urbanization["GeoFIPS"] = urbanization["STATE"].astype(str) + urbanization[ | ||
"COUNTY" | ||
].astype(str) | ||
urbanization["GeoFIPS"] = urbanization["GeoFIPS"].astype(int) | ||
|
||
common_fips = np.intersect1d( | ||
gdp["GeoFIPS"].unique(), urbanization["GeoFIPS"].unique() | ||
) | ||
|
||
urbanization = urbanization[urbanization["GeoFIPS"].isin(common_fips)] | ||
|
||
urbanization = urbanization.merge( | ||
gdp[["GeoFIPS", "GeoName"]], on="GeoFIPS", how="left" | ||
) | ||
|
||
urbanization = urbanization[ | ||
[ | ||
"GeoFIPS", | ||
"GeoName", | ||
"POPDEN_RUR", | ||
"POPDEN_URB", | ||
"HOUDEN_COU", | ||
"HOUDEN_RUR", | ||
"ALAND_PCT_RUR", | ||
] | ||
] | ||
|
||
urbanization = urbanization.sort_values(by=["GeoFIPS", "GeoName"]) | ||
|
||
urbanization_wide = urbanization.copy() | ||
|
||
urbanization_long = pd.melt( | ||
urbanization, | ||
id_vars=["GeoFIPS", "GeoName"], | ||
var_name="Category", | ||
value_name="Value", | ||
) | ||
|
||
urbanization_std_wide = standardize_and_scale(urbanization) | ||
|
||
urbanization_std_long = pd.melt( | ||
urbanization_std_wide.copy(), | ||
id_vars=["GeoFIPS", "GeoName"], | ||
var_name="Category", | ||
value_name="Value", | ||
) | ||
|
||
urbanization_wide.to_csv("../data/processed/urbanization_wide.csv", index=False) | ||
urbanization_long.to_csv("../data/processed/urbanization_long.csv", index=False) | ||
urbanization_std_wide.to_csv( | ||
"../data/processed/urbanization_std_wide.csv", index=False | ||
) | ||
urbanization_std_long.to_csv( | ||
"../data/processed/urbanization_std_long.csv", index=False | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.