Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download batch_scripts/census_county_data.py from Climate-Lab/climate-policy-tracker: direct link, hf CLI and curl.
- Browser
- Download file 761 Bytes
-
https://huggingface.co/spaces/Climate-Lab/climate-policy-tracker/resolve/cedddaeeffb97c2ff2efc2f9c479335afd44e25e/batch_scripts/census_county_data.py
- Command line
-
hf download hf://spaces/Climate-Lab/climate-policy-tracker@cedddaeeffb97c2ff2efc2f9c479335afd44e25e/batch_scripts/census_county_data.py
-
curl -L -o census_county_data.py https://huggingface.co/spaces/Climate-Lab/climate-policy-tracker/resolve/cedddaeeffb97c2ff2efc2f9c479335afd44e25e/batch_scripts/census_county_data.py
761 Bytes
| import requests | |
| import pandas as pd | |
| API_KEY = "ea9637fd9f0c41f3e2e932faa99dfcd76f8041aa" | |
| def fetch_county_data(): | |
| """Fetch county-level Census data, return as DataFrame.""" | |
| county_url = ( | |
| f"https://api.census.gov/data/2019/pep/population" | |
| f"?get=NAME&for=county:*&key={API_KEY}" | |
| ) | |
| r_counties = requests.get(county_url) | |
| county_data = r_counties.json() | |
| df = pd.DataFrame(county_data[1:], columns=county_data[0]) | |
| # Split the NAME column into state and county | |
| df[['countyName', 'stateName']] = df['NAME'].str.split(',', expand=True) | |
| # Combine state and county to form FIPS | |
| df["FIPS"] = df["state"].str.zfill(2) + df["county"].str.zfill(3) | |
| df.to_csv("./us_counties.csv", index=False) | |
| fetch_county_data() |