Last active
March 8, 2021 18:37
-
-
Save scottpham/49b2ce19b5c31623cbe2a6e32ad3f7e1 to your computer and use it in GitHub Desktop.
How to download census data in Python
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
import censusdata | |
import pandas as pd | |
# https://towardsdatascience.com/accessing-census-data-with-python-3e2f2b56e20d | |
# https://jtleider.github.io/censusdata/ | |
# search for the right table | |
sample = censusdata.search('acs1', 2019, 'concept', 'total population') | |
sample[0] | |
# check all the sub-tables -- this one just has one | |
pd.DataFrame( | |
censusdata.censustable('acs1', 2019, 'B01003') | |
).T | |
# do this if you need a specific state by FIPS code | |
states = censusdata.geographies(censusdata.censusgeo([('state', '*')]),'acs1', 2019) | |
states | |
# state example | |
censusdata.download( | |
'acs1', | |
2019, | |
censusdata.censusgeo([('state', '39')]), | |
['B01003_001E'] | |
)['B01003_001E'][0] | |
# download the whole thing for all states | |
census_dl = censusdata.download( | |
'acs1', | |
2019, | |
censusdata.censusgeo([('state', '*')]), | |
['B01003_001E'] | |
) | |
# fix because the index is wonky | |
census = ( | |
census_dl | |
.reset_index() | |
.assign( | |
fips_code = lambda x: x['index'].apply( | |
lambda x: str(x)[-2:] | |
) | |
) | |
) | |
census.dtypes | |
df = pd.read_csv( | |
"../csvs/2018_agency.csv", | |
dtype = { | |
"StFIPS": "str" | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment