I want to get ACS1 tables from 2010 to 2021 for B25077_001E variable for NY, NJ at the county subdivision and place level to include only the name of the community, geoid and year in python and store it as a pandas dataframe. Could you please help? Thank you!
import requests
import os
import pandas as pd
inputKeyPath r””
inputKeyName = ‘CensusKey.txt’
f = open(inputKeyPath, “r”)
myKey = f.readline()
var_list = [‘B25077_001E’]
endpoint = ‘acs/acs1’
prefix_url = ‘https://api.census.gov/data’
states_terr = [‘34’, ‘36’]
states_in = ‘.’.join(states_terr)
years = range(2010, 2022)
geographies = [‘place’, ‘county subdivision’]
final_df_list = []
for geography in geographies:
df_list = []
for year in years:
base_url = “/“.join([prefix_url, year, endpoint])
predicates = {}
get_vars = [‘NAME’, ‘GEO_ID’] + var_list
predicates[“get”] = ‘,’.join(get_vars)
predicates[“for”] = f’{geography}:*’
predicates[“in”] = f’state:{states_in}’
predicates[“key”] = myKey
r = requests.get(base_url, params=predicates
temp_df = pd.DataFrame(r.json())
final_df = pd.DataFrame(r.json()[1:], column=list(temp_df.loc[0]))
final_df[‘year’] = year
final_df.set_index(‘GEO_ID’, inplace=True)
df_list.append(final_df[[i for i in final_df.columns if i not in [‘state’, ‘county’, geography]]])
sub_geo_df = pd.concat(df_list, axis=1)
final_df_list.append(sub_geo_df([[i for i in sub_geo_df.columns if i not in [‘state’, ‘county’, geography]]])