Skip to content

Instantly share code, notes, and snippets.

@afcotroneo
Last active November 17, 2021 15:12
Show Gist options
  • Save afcotroneo/49340717b5df1b81dff4e779560b9785 to your computer and use it in GitHub Desktop.
Save afcotroneo/49340717b5df1b81dff4e779560b9785 to your computer and use it in GitHub Desktop.
STATEFIPS = ["01","04","05","06","08","09","10","12","13","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","44","45","46","47","48","49","50","51","53","54","55","56"]
col_names = ["white_pop", "black_pop","asian_pop", "amerindian_pop","hispanic_pop", "other_race_pop", "two_or_more_races_pop", "total_pop", "state","county","tract","block_group"]
for st_fips in STATEFIPS:
# Get these fields of data for the year 2000
r2000 = requests.get("https://api.census.gov/data/2000/dec/sf1?get=P004005,P004006,P004008,P004007,P004002,P004010,P004011,P004001&for=block%20group:*&in=state:{}&in=county:*&in=tract:*&key=30699f15ab4d04a1e0943715b539d256c9a3ee44".format(st_fips))
# Create a data frame with the results
df = pd.DataFrame(columns=col_names, data=r2000.json()[1:])
# Each point needs to have a date column so that we can tell the difference between years
df["year"] = '2000-01-01T00:12:00.000Z'
# Creating a new field to make it easier to join to the geometry
df['geoconcat'] = df['state'] + df['county'] + df['tract'] + df['block_group']
# Get the 2000 census block groups for that state for that year and create the same field for easier joining
zipped_shp_url = 'https://www2.census.gov/geo/tiger/PREVGENZ/bg/bg00shp/bg{}_d00_shp.zip'.format(st_fips)
gdf = gpd.read_file(zipped_shp_url)
gdf['geoconcat'] = gdf['STATE'] + gdf['COUNTY'] + gdf['TRACT'] + gdf['BLKGROUP']
# Join/Merge on the geoconcat join key
df = pd.merge(df,gdf[['geometry','geoconcat']],on='geoconcat', how='left')
# There were 23 out of ~200000 that couldn't be joined and are dropped
df = df[df['geometry'].notna()]
# Convert object fields to numeric
df["white_pop"] = pd.to_numeric(df["white_pop"])
df["black_pop"] = pd.to_numeric(df["black_pop"])
df["hispanic_pop"] = pd.to_numeric(df["hispanic_pop"])
df["asian_pop"] = pd.to_numeric(df["asian_pop"])
df["amerindian_pop"] = pd.to_numeric(df["amerindian_pop"])
df["other_race_pop"] = pd.to_numeric(df["other_race_pop"])
df["two_or_more_races_pop"] = pd.to_numeric(df["two_or_more_races_pop"])
df["total_pop"] = pd.to_numeric(df["total_pop"])
# Run the Dot Density Creation Functions
for chunk in np.array_split(df, df.shape[0]):
dot_density_and_pushtoomnisci(chunk,'2000-01-01T00:12:00.000Z',50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment