import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from IPython.core.display import display, HTML
%matplotlib inline
from zipfile import ZipFile
import geopandas as gpd
from shapely.geometry import Point
pd.options.display.max_columns = None
display(HTML("<style>.container { width:100% !important; }</style>"))
ZIP_SHP_PATH = os.path.join('zip://', 'data', 'shapefiles', 'Boundaries - Census Blocks - 2010.zip')
coord_system = {'init': 'epsg:4326'}
chicago_census_blocks = gpd.read_file(ZIP_SHP_PATH).to_crs(coord_system)
chicago_census_blocks['geoid10'] = chicago_census_blocks['geoid10'].astype(str)
chicago_census_blocks.plot(color='white', edgecolor='black', figsize=(25,25))
<matplotlib.axes._subplots.AxesSubplot at 0x1ced1ab84a8>
ZIP_SHP_PATH = os.path.join('zip://', 'data', 'shapefiles', 'Boundaries - Police Beats (current).zip')
coord_system = {'init': 'epsg:4326'}
chicago_police_beats = gpd.read_file(ZIP_SHP_PATH).to_crs(coord_system)
chicago_police_beats.plot(color='white', edgecolor='black', figsize=(15,15))
<matplotlib.axes._subplots.AxesSubplot at 0x1ced236b2b0>
ZIP_SHP_PATH = os.path.join('zip://', 'data', 'shapefiles', 'Boundaries - Police Districts (current).zip')
coord_system = {'init': 'epsg:4326'}
chicago_police_districts = gpd.read_file(ZIP_SHP_PATH).to_crs(coord_system)
chicago_police_districts.plot(color='white', edgecolor='black', figsize=(15,15))
<matplotlib.axes._subplots.AxesSubplot at 0x1ced2310a90>
ZIP_SHP_PATH = os.path.join('zip://', 'data', 'shapefiles', 'cb_2016_17_bg_500k.zip')
coord_system = {'init': 'epsg:4326'}
cook_census_blocks = gpd.read_file(ZIP_SHP_PATH).to_crs(coord_system)
cook_census_blocks = cook_census_blocks[cook_census_blocks['COUNTYFP'] == '031']
cook_census_blocks['GEOID'] = cook_census_blocks['GEOID'].astype(str)
cook_census_blocks.plot(color='white', edgecolor='black', figsize=(25,25))
<matplotlib.axes._subplots.AxesSubplot at 0x1ced1204278>
ZIP_SHP_PATH = os.path.join('zip://', 'data', 'shapefiles', 'Boundaries - City.zip')
coord_system = {'init': 'epsg:4326'}
chicago = gpd.read_file(ZIP_SHP_PATH).to_crs(coord_system)
chicago.drop(['shape_len', 'shape_area', 'objectid', 'name'], axis=1, inplace=True)
chicago.plot(color='white', edgecolor='black', figsize=(10,10))
<matplotlib.axes._subplots.AxesSubplot at 0x1cee5bcc278>
chicagoish = gpd.sjoin(left_df=cook_census_blocks, right_df=chicago, how='inner', op='intersects')
chicagoish.plot(color='white', edgecolor='black', figsize=(15,15))
<matplotlib.axes._subplots.AxesSubplot at 0x1ced412e9b0>
chicagoish.head()
STATEFP | COUNTYFP | TRACTCE | BLKGRPCE | AFFGEOID | GEOID | NAME | LSAD | ALAND | AWATER | geometry | index_right | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 17 | 031 | 010100 | 3 | 1500000US170310101003 | 170310101003 | 3 | BG | 133973 | 0 | POLYGON ((-87.669479 42.021143, -87.666584 42.... | 0 |
11 | 17 | 031 | 140800 | 5 | 1500000US170311408005 | 170311408005 | 5 | BG | 226961 | 5584 | POLYGON ((-87.708472 41.968452, -87.703605 41.... | 0 |
14 | 17 | 031 | 681000 | 3 | 1500000US170316810003 | 170316810003 | 3 | BG | 122400 | 0 | POLYGON ((-87.651977 41.776006, -87.648336 41.... | 0 |
16 | 17 | 031 | 252000 | 3 | 1500000US170312520003 | 170312520003 | 3 | BG | 180120 | 0 | POLYGON ((-87.774782 41.884132, -87.7698949999... | 0 |
19 | 17 | 031 | 661100 | 2 | 1500000US170316611002 | 170316611002 | 2 | BG | 571658 | 0 | POLYGON ((-87.71253999999999 41.764242, -87.71... | 0 |
ZIP_PATH = os.path.join('data', 'census', 'DEC_10_PL_P1.zip')
with ZipFile(ZIP_PATH) as zip_file:
with zip_file.open('DEC_10_PL_P1_with_ann.csv') as my_zipped_csv:
cook_acs_df = pd.read_csv(my_zipped_csv, header=1, dtype={'Id2':str})
cook_acs_df.head()
Id | Id2 | Geography | Total: | Population of one race: | Population of one race: - White alone | Population of one race: - Black or African American alone | Population of one race: - American Indian and Alaska Native alone | Population of one race: - Asian alone | Population of one race: - Native Hawaiian and Other Pacific Islander alone | Population of one race: - Some Other Race alone | Two or More Races: | Two or More Races: - Population of two races: | Two or More Races: - Population of two races: - White; Black or African American | Two or More Races: - Population of two races: - White; American Indian and Alaska Native | Two or More Races: - Population of two races: - White; Asian | Two or More Races: - Population of two races: - White; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - White; Some Other Race | Two or More Races: - Population of two races: - Black or African American; American Indian and Alaska Native | Two or More Races: - Population of two races: - Black or African American; Asian | Two or More Races: - Population of two races: - Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Black or African American; Some Other Race | Two or More Races: - Population of two races: - American Indian and Alaska Native; Asian | Two or More Races: - Population of two races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of two races: - Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Asian; Some Other Race | Two or More Races: - Population of two races: - Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: | Two or More Races: - Population of three races: - White; Black or African American; American Indian and Alaska Native | Two or More Races: - Population of three races: - White; Black or African American; Asian | Two or More Races: - Population of three races: - White; Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Black or African American; Some Other Race | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - White; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Asian; Some Other Race | Two or More Races: - Population of three races: - White; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; Asian; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; Asian; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of six races: | Two or More Races: - Population of six races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1500000US170310101001 | 170310101001 | Block Group 1, Census Tract 101, Cook County, ... | 579 | 557 | 323 | 171 | 8 | 21 | 0 | 34 | 22 | 19 | 5 | 1 | 6 | 0 | 0 | 0 | 0 | 1 | 4 | 1 | 0 | 0 | 0 | 1 | 0 | 3 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 1500000US170310101002 | 170310101002 | Block Group 2, Census Tract 101, Cook County, ... | 2311 | 2216 | 439 | 1621 | 20 | 36 | 1 | 99 | 95 | 82 | 34 | 4 | 3 | 0 | 5 | 14 | 3 | 2 | 16 | 1 | 0 | 0 | 0 | 0 | 0 | 12 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 1 | 0 | 2 | 0 | 1 | 3 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 1500000US170310101003 | 170310101003 | Block Group 3, Census Tract 101, Cook County, ... | 1964 | 1861 | 1048 | 645 | 5 | 93 | 0 | 70 | 103 | 96 | 25 | 4 | 23 | 0 | 17 | 8 | 7 | 0 | 8 | 0 | 0 | 0 | 0 | 4 | 0 | 6 | 3 | 2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 1500000US170310102011 | 170310102011 | Block Group 1, Census Tract 102.01, Cook Count... | 1445 | 1352 | 516 | 580 | 5 | 94 | 1 | 156 | 93 | 78 | 17 | 5 | 19 | 0 | 18 | 2 | 2 | 1 | 4 | 0 | 0 | 2 | 5 | 3 | 0 | 11 | 6 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 2 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4 | 1500000US170310102012 | 170310102012 | Block Group 2, Census Tract 102.01, Cook Count... | 3872 | 3678 | 1254 | 1493 | 36 | 142 | 5 | 748 | 194 | 168 | 52 | 4 | 20 | 3 | 27 | 23 | 10 | 2 | 15 | 9 | 0 | 1 | 1 | 0 | 1 | 22 | 13 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 4 | 1 | 1 | 0 | 0 | 0 | 0 | 4 | 0 | 0 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
print('cook_census_blocks.shape: {}'.format(cook_census_blocks.shape))
print('cook_acs_df.shape: {}'.format(cook_acs_df.shape))
cook_census_blocks.shape: (3992, 11) cook_acs_df.shape: (3994, 75)
cook_census_blocks.head()
STATEFP | COUNTYFP | TRACTCE | BLKGRPCE | AFFGEOID | GEOID | NAME | LSAD | ALAND | AWATER | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 17 | 031 | 010100 | 3 | 1500000US170310101003 | 170310101003 | 3 | BG | 133973 | 0 | POLYGON ((-87.669479 42.021143, -87.666584 42.... |
2 | 17 | 031 | 811301 | 4 | 1500000US170318113014 | 170318113014 | 4 | BG | 1626156 | 0 | POLYGON ((-87.893986 41.91969, -87.890474 41.9... |
7 | 17 | 031 | 824123 | 1 | 1500000US170318241231 | 170318241231 | 1 | BG | 1086582 | 11903 | POLYGON ((-87.80476499999999 41.58991, -87.803... |
8 | 17 | 031 | 819900 | 2 | 1500000US170318199002 | 170318199002 | 2 | BG | 1069519 | 0 | POLYGON ((-87.913223 41.811342, -87.9116790000... |
11 | 17 | 031 | 140800 | 5 | 1500000US170311408005 | 170311408005 | 5 | BG | 226961 | 5584 | POLYGON ((-87.708472 41.968452, -87.703605 41.... |
cook_gdf = pd.merge(left=cook_census_blocks, right=cook_acs_df, left_on='GEOID', right_on='Id2', how='inner')
cook_gdf.head(2)
STATEFP | COUNTYFP | TRACTCE | BLKGRPCE | AFFGEOID | GEOID | NAME | LSAD | ALAND | AWATER | geometry | Id | Id2 | Geography | Total: | Population of one race: | Population of one race: - White alone | Population of one race: - Black or African American alone | Population of one race: - American Indian and Alaska Native alone | Population of one race: - Asian alone | Population of one race: - Native Hawaiian and Other Pacific Islander alone | Population of one race: - Some Other Race alone | Two or More Races: | Two or More Races: - Population of two races: | Two or More Races: - Population of two races: - White; Black or African American | Two or More Races: - Population of two races: - White; American Indian and Alaska Native | Two or More Races: - Population of two races: - White; Asian | Two or More Races: - Population of two races: - White; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - White; Some Other Race | Two or More Races: - Population of two races: - Black or African American; American Indian and Alaska Native | Two or More Races: - Population of two races: - Black or African American; Asian | Two or More Races: - Population of two races: - Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Black or African American; Some Other Race | Two or More Races: - Population of two races: - American Indian and Alaska Native; Asian | Two or More Races: - Population of two races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of two races: - Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Asian; Some Other Race | Two or More Races: - Population of two races: - Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: | Two or More Races: - Population of three races: - White; Black or African American; American Indian and Alaska Native | Two or More Races: - Population of three races: - White; Black or African American; Asian | Two or More Races: - Population of three races: - White; Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Black or African American; Some Other Race | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - White; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Asian; Some Other Race | Two or More Races: - Population of three races: - White; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; Asian; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; Asian; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of six races: | Two or More Races: - Population of six races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | tmp_id | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 17 | 031 | 010100 | 3 | 1500000US170310101003 | 170310101003 | 3 | BG | 133973 | 0 | POLYGON ((-87.669479 42.021143, -87.666584 42.... | 1500000US170310101003 | 170310101003 | Block Group 3, Census Tract 101, Cook County, ... | 1964 | 1861 | 1048 | 645 | 5 | 93 | 0 | 70 | 103 | 96 | 25 | 4 | 23 | 0 | 17 | 8 | 7 | 0 | 8 | 0 | 0 | 0 | 0 | 4 | 0 | 6 | 3 | 2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0101003 |
1 | 17 | 031 | 811301 | 4 | 1500000US170318113014 | 170318113014 | 4 | BG | 1626156 | 0 | POLYGON ((-87.893986 41.91969, -87.890474 41.9... | 1500000US170318113014 | 170318113014 | Block Group 4, Census Tract 8113.01, Cook Coun... | 1384 | 1347 | 1053 | 8 | 1 | 41 | 0 | 244 | 37 | 35 | 1 | 1 | 9 | 0 | 19 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 2 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8113014 |
chiish_gdf = pd.merge(left=chicagoish, right=cook_acs_df, left_on='GEOID', right_on='Id2', how='inner')
chiish_gdf.head(2)
STATEFP | COUNTYFP | TRACTCE | BLKGRPCE | AFFGEOID | GEOID | NAME | LSAD | ALAND | AWATER | geometry | index_right | Id | Id2 | Geography | Total: | Population of one race: | Population of one race: - White alone | Population of one race: - Black or African American alone | Population of one race: - American Indian and Alaska Native alone | Population of one race: - Asian alone | Population of one race: - Native Hawaiian and Other Pacific Islander alone | Population of one race: - Some Other Race alone | Two or More Races: | Two or More Races: - Population of two races: | Two or More Races: - Population of two races: - White; Black or African American | Two or More Races: - Population of two races: - White; American Indian and Alaska Native | Two or More Races: - Population of two races: - White; Asian | Two or More Races: - Population of two races: - White; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - White; Some Other Race | Two or More Races: - Population of two races: - Black or African American; American Indian and Alaska Native | Two or More Races: - Population of two races: - Black or African American; Asian | Two or More Races: - Population of two races: - Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Black or African American; Some Other Race | Two or More Races: - Population of two races: - American Indian and Alaska Native; Asian | Two or More Races: - Population of two races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of two races: - Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Asian; Some Other Race | Two or More Races: - Population of two races: - Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: | Two or More Races: - Population of three races: - White; Black or African American; American Indian and Alaska Native | Two or More Races: - Population of three races: - White; Black or African American; Asian | Two or More Races: - Population of three races: - White; Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Black or African American; Some Other Race | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - White; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Asian; Some Other Race | Two or More Races: - Population of three races: - White; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; Asian; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; Asian; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of six races: | Two or More Races: - Population of six races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | tmp_id | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 17 | 031 | 010100 | 3 | 1500000US170310101003 | 170310101003 | 3 | BG | 133973 | 0 | POLYGON ((-87.669479 42.021143, -87.666584 42.... | 0 | 1500000US170310101003 | 170310101003 | Block Group 3, Census Tract 101, Cook County, ... | 1964 | 1861 | 1048 | 645 | 5 | 93 | 0 | 70 | 103 | 96 | 25 | 4 | 23 | 0 | 17 | 8 | 7 | 0 | 8 | 0 | 0 | 0 | 0 | 4 | 0 | 6 | 3 | 2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0101003 |
1 | 17 | 031 | 140800 | 5 | 1500000US170311408005 | 170311408005 | 5 | BG | 226961 | 5584 | POLYGON ((-87.708472 41.968452, -87.703605 41.... | 0 | 1500000US170311408005 | 170311408005 | Block Group 5, Census Tract 1408, Cook County,... | 2239 | 2156 | 1283 | 81 | 11 | 253 | 2 | 526 | 83 | 80 | 14 | 7 | 18 | 0 | 26 | 3 | 1 | 0 | 0 | 3 | 0 | 0 | 1 | 6 | 1 | 2 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1408005 |
chiish_gdf.shape
(2325, 87)
That outlier block in the center of the map is where the Cook County Department of Corrections facility is located, hence the higher population density.
fig, ax = plt.subplots(figsize=(15,15))
my_cmap='BuGn'
col_ = 'Total:'
df = chiish_gdf
vmin = df[col_].min()
vmax = df[col_].max()
base = df.plot(color='white', edgecolor='black', ax=ax)
_ = df.plot(column=col_, cmap=my_cmap, ax=base)
_ = ax.axis('off')
_ = ax.set_title('Population feature: {}'.format(col_),
fontdict={'fontsize': '25', 'fontweight' : '3'})
_ = ax.annotate('Source: American Community Survey, 2016',
xy=(0.1, .1), xycoords='figure fraction',
horizontalalignment='left', verticalalignment='top',
fontsize=10, color='#555555')
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=vmin, vmax=vmax))
sm._A = []
cbar = fig.colorbar(sm, shrink=0.5)
plt.tight_layout()
prefix_stripper = lambda x: x[5:]
cook_acs_df['tmp_id'] = cook_acs_df['Id2'].apply(prefix_stripper)
chicago_census_blocks['tmp_geoid'] = chicago_census_blocks['geoid10'].apply(prefix_stripper)
chicago_census_blocks.head()
blockce10 | countyfp10 | geoid10 | name10 | statefp10 | tract_bloc | tractce10 | geometry | tmp_geoid | |
---|---|---|---|---|---|---|---|---|---|
0 | 2010 | 031 | 170316903002010 | Block 2010 | 17 | 6903002010 | 690300 | POLYGON ((-87.62906799941059 41.76908600007152... | 6903002010 |
1 | 3007 | 031 | 170316809003007 | Block 3007 | 17 | 6809003007 | 680900 | POLYGON ((-87.6341179992764 41.77446599958471,... | 6809003007 |
2 | 3013 | 031 | 170316809003013 | Block 3013 | 17 | 6809003013 | 680900 | POLYGON ((-87.63485400018324 41.77263300022619... | 6809003013 |
3 | 4019 | 031 | 170312909004019 | Block 4019 | 17 | 2909004019 | 290900 | POLYGON ((-87.73841099998789 41.85913100037079... | 2909004019 |
4 | 4016 | 031 | 170312925004016 | Block 4016 | 17 | 2925004016 | 292500 | POLYGON ((-87.73217300001724 41.85476400046529... | 2925004016 |
tmp = pd.merge(left=chicago_census_blocks, right=cook_acs_df, left_on='tmp_geoid', right_on='tmp_id', how='inner')
tmp.head()
blockce10 | countyfp10 | geoid10 | name10 | statefp10 | tract_bloc | tractce10 | geometry | tmp_geoid | Id | Id2 | Geography | Total: | Population of one race: | Population of one race: - White alone | Population of one race: - Black or African American alone | Population of one race: - American Indian and Alaska Native alone | Population of one race: - Asian alone | Population of one race: - Native Hawaiian and Other Pacific Islander alone | Population of one race: - Some Other Race alone | Two or More Races: | Two or More Races: - Population of two races: | Two or More Races: - Population of two races: - White; Black or African American | Two or More Races: - Population of two races: - White; American Indian and Alaska Native | Two or More Races: - Population of two races: - White; Asian | Two or More Races: - Population of two races: - White; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - White; Some Other Race | Two or More Races: - Population of two races: - Black or African American; American Indian and Alaska Native | Two or More Races: - Population of two races: - Black or African American; Asian | Two or More Races: - Population of two races: - Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Black or African American; Some Other Race | Two or More Races: - Population of two races: - American Indian and Alaska Native; Asian | Two or More Races: - Population of two races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of two races: - Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Asian; Some Other Race | Two or More Races: - Population of two races: - Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: | Two or More Races: - Population of three races: - White; Black or African American; American Indian and Alaska Native | Two or More Races: - Population of three races: - White; Black or African American; Asian | Two or More Races: - Population of three races: - White; Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Black or African American; Some Other Race | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - White; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Asian; Some Other Race | Two or More Races: - Population of three races: - White; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; Asian; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; Asian; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of six races: | Two or More Races: - Population of six races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | tmp_id |
---|
tmp = pd.merge(left=chicago_census_blocks, right=cook_acs_df, left_on='geoid10', right_on='Id2', how='inner')
tmp.head()
blockce10 | countyfp10 | geoid10 | name10 | statefp10 | tract_bloc | tractce10 | geometry | tmp_geoid | Id | Id2 | Geography | Total: | Population of one race: | Population of one race: - White alone | Population of one race: - Black or African American alone | Population of one race: - American Indian and Alaska Native alone | Population of one race: - Asian alone | Population of one race: - Native Hawaiian and Other Pacific Islander alone | Population of one race: - Some Other Race alone | Two or More Races: | Two or More Races: - Population of two races: | Two or More Races: - Population of two races: - White; Black or African American | Two or More Races: - Population of two races: - White; American Indian and Alaska Native | Two or More Races: - Population of two races: - White; Asian | Two or More Races: - Population of two races: - White; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - White; Some Other Race | Two or More Races: - Population of two races: - Black or African American; American Indian and Alaska Native | Two or More Races: - Population of two races: - Black or African American; Asian | Two or More Races: - Population of two races: - Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Black or African American; Some Other Race | Two or More Races: - Population of two races: - American Indian and Alaska Native; Asian | Two or More Races: - Population of two races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of two races: - Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Asian; Some Other Race | Two or More Races: - Population of two races: - Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: | Two or More Races: - Population of three races: - White; Black or African American; American Indian and Alaska Native | Two or More Races: - Population of three races: - White; Black or African American; Asian | Two or More Races: - Population of three races: - White; Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Black or African American; Some Other Race | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - White; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Asian; Some Other Race | Two or More Races: - Population of three races: - White; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; Asian; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; Asian; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of six races: | Two or More Races: - Population of six races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | tmp_id |
---|
len("0101001")
7
cook_acs_df.loc[550, 'Id2']
'170311505012'
cook_acs_df.loc[550, 'Id2'][5:]
'1505012'
cook_acs_df['Id2'] = cook_acs_df['Id2'].astype(str)
chicago_census_blocks.head()
blockce10 | countyfp10 | geoid10 | name10 | statefp10 | tract_bloc | tractce10 | geometry | |
---|---|---|---|---|---|---|---|---|
0 | 2010 | 031 | 170316903002010 | Block 2010 | 17 | 6903002010 | 690300 | POLYGON ((-87.62906799941059 41.76908600007152... |
1 | 3007 | 031 | 170316809003007 | Block 3007 | 17 | 6809003007 | 680900 | POLYGON ((-87.6341179992764 41.77446599958471,... |
2 | 3013 | 031 | 170316809003013 | Block 3013 | 17 | 6809003013 | 680900 | POLYGON ((-87.63485400018324 41.77263300022619... |
3 | 4019 | 031 | 170312909004019 | Block 4019 | 17 | 2909004019 | 290900 | POLYGON ((-87.73841099998789 41.85913100037079... |
4 | 4016 | 031 | 170312925004016 | Block 4016 | 17 | 2925004016 | 292500 | POLYGON ((-87.73217300001724 41.85476400046529... |
chicago_census_blocks['name10'].unique()
array(['Block 2010', 'Block 3007', 'Block 3013', 'Block 4019', 'Block 4016', 'Block 4000', 'Block 2003', 'Block 2002', 'Block 3019', 'Block 3005', 'Block 3021', 'Block 1034', 'Block 2042', 'Block 1020', 'Block 1021', 'Block 1049', 'Block 4007', 'Block 4011', 'Block 3008', 'Block 1022', 'Block 1012', 'Block 4013', 'Block 3010', 'Block 1001', 'Block 1032', 'Block 1017', 'Block 2031', 'Block 2019', 'Block 6010', 'Block 1002', 'Block 3025', 'Block 2044', 'Block 3014', 'Block 1004', 'Block 1011', 'Block 2025', 'Block 2011', 'Block 1023', 'Block 1008', 'Block 1006', 'Block 1027', 'Block 1018', 'Block 1000', 'Block 2009', 'Block 3004', 'Block 5022', 'Block 3017', 'Block 2013', 'Block 2008', 'Block 4001', 'Block 2023', 'Block 2028', 'Block 4015', 'Block 4002', 'Block 4027', 'Block 4040', 'Block 2012', 'Block 2001', 'Block 2059', 'Block 2049', 'Block 5004', 'Block 1015', 'Block 4096', 'Block 4109', 'Block 1007', 'Block 1026', 'Block 2004', 'Block 2015', 'Block 2027', 'Block 1067', 'Block 3016', 'Block 3001', 'Block 2006', 'Block 3031', 'Block 3038', 'Block 4021', 'Block 4009', 'Block 3029', 'Block 2014', 'Block 1010', 'Block 5006', 'Block 5034', 'Block 5016', 'Block 4026', 'Block 1025', 'Block 1064', 'Block 1033', 'Block 3003', 'Block 5008', 'Block 5014', 'Block 3009', 'Block 1037', 'Block 3018', 'Block 5007', 'Block 1029', 'Block 2022', 'Block 2005', 'Block 2017', 'Block 5015', 'Block 1014', 'Block 3002', 'Block 2036', 'Block 4005', 'Block 4014', 'Block 4053', 'Block 2018', 'Block 5011', 'Block 2007', 'Block 4003', 'Block 1009', 'Block 3006', 'Block 5005', 'Block 5003', 'Block 1019', 'Block 3011', 'Block 7016', 'Block 6000', 'Block 4004', 'Block 5027', 'Block 5010', 'Block 4022', 'Block 5035', 'Block 6007', 'Block 5001', 'Block 1003', 'Block 4024', 'Block 1005', 'Block 1024', 'Block 1045', 'Block 4006', 'Block 3000', 'Block 2000', 'Block 1041', 'Block 2030', 'Block 1091', 'Block 1036', 'Block 2035', 'Block 2021', 'Block 2016', 'Block 4029', 'Block 1016', 'Block 1043', 'Block 2024', 'Block 2029', 'Block 4008', 'Block 4010', 'Block 4012', 'Block 3015', 'Block 5018', 'Block 3026', 'Block 1013', 'Block 6017', 'Block 2026', 'Block 2043', 'Block 2032', 'Block 2020', 'Block 3034', 'Block 5017', 'Block 6002', 'Block 1066', 'Block 5002', 'Block 1063', 'Block 3036', 'Block 1028', 'Block 1089', 'Block 2051', 'Block 2045', 'Block 4059', 'Block 4063', 'Block 4054', 'Block 5000', 'Block 1031', 'Block 1080', 'Block 2033', 'Block 5012', 'Block 5009', 'Block 4035', 'Block 1030', 'Block 2073', 'Block 3023', 'Block 6013', 'Block 4055', 'Block 1046', 'Block 4032', 'Block 2048', 'Block 1039', 'Block 3012', 'Block 3039', 'Block 6004', 'Block 3020', 'Block 4017', 'Block 4023', 'Block 6009', 'Block 5021', 'Block 6005', 'Block 6001', 'Block 6008', 'Block 1051', 'Block 1044', 'Block 1061', 'Block 1074', 'Block 3024', 'Block 1053', 'Block 1042', 'Block 1058', 'Block 2040', 'Block 1068', 'Block 2038', 'Block 4050', 'Block 3022', 'Block 1038', 'Block 3033', 'Block 3027', 'Block 5019', 'Block 3028', 'Block 6025', 'Block 2108', 'Block 5013', 'Block 2037', 'Block 1060', 'Block 1050', 'Block 2050', 'Block 3035', 'Block 6018', 'Block 2105', 'Block 3041', 'Block 2041', 'Block 4018', 'Block 7013', 'Block 1040', 'Block 1035', 'Block 4020', 'Block 5020', 'Block 2102', 'Block 2109', 'Block 4031', 'Block 4033', 'Block 3032', 'Block 6020', 'Block 6032', 'Block 4038', 'Block 4036', 'Block 2063', 'Block 1055', 'Block 5030', 'Block 5025', 'Block 4030', 'Block 4052', 'Block 4025', 'Block 2092', 'Block 2085', 'Block 6003', 'Block 5036', 'Block 5039', 'Block 2056', 'Block 2046', 'Block 3052', 'Block 3050', 'Block 4042', 'Block 4041', 'Block 5033', 'Block 5024', 'Block 4051', 'Block 4047', 'Block 4046', 'Block 1054', 'Block 6029', 'Block 2055', 'Block 2047', 'Block 1082', 'Block 1079', 'Block 1086', 'Block 1102', 'Block 1101', 'Block 2058', 'Block 1047', 'Block 2039', 'Block 2081', 'Block 4043', 'Block 4037', 'Block 2053', 'Block 2052', 'Block 7014', 'Block 7012', 'Block 6036', 'Block 2094', 'Block 4069', 'Block 4049', 'Block 2111', 'Block 2163', 'Block 2090', 'Block 2230', 'Block 2034', 'Block 2072', 'Block 1070', 'Block 1093', 'Block 4039', 'Block 6031', 'Block 3040', 'Block 2215', 'Block 6012', 'Block 6015', 'Block 7015', 'Block 2060', 'Block 4034', 'Block 2054', 'Block 4082', 'Block 7001', 'Block 2106', 'Block 2212', 'Block 4066', 'Block 3048', 'Block 2097', 'Block 1085', 'Block 1088', 'Block 5026', 'Block 4067', 'Block 6011', 'Block 6021', 'Block 1057', 'Block 2082', 'Block 2217', 'Block 2113', 'Block 4028', 'Block 2124', 'Block 6028', 'Block 5059', 'Block 3042', 'Block 2068', 'Block 4048', 'Block 4058', 'Block 1052', 'Block 2103', 'Block 2119', 'Block 2228', 'Block 5023', 'Block 2062', 'Block 1062', 'Block 3045', 'Block 5029', 'Block 1100', 'Block 2069', 'Block 3037', 'Block 2080', 'Block 2084', 'Block 2179', 'Block 2154', 'Block 2155', 'Block 2168', 'Block 2057', 'Block 6019', 'Block 3055', 'Block 3053', 'Block 3046', 'Block 2087', 'Block 2093', 'Block 2070', 'Block 3049', 'Block 2192', 'Block 2161', 'Block 2149', 'Block 2133', 'Block 2131', 'Block 2091', 'Block 5028', 'Block 2098', 'Block 2075', 'Block 7017', 'Block 2071', 'Block 3030', 'Block 2099', 'Block 2115', 'Block 2086', 'Block 2067', 'Block 2209', 'Block 2078', 'Block 2076', 'Block 2061', 'Block 1096', 'Block 1077', 'Block 1073', 'Block 1048', 'Block 1084', 'Block 3047', 'Block 2117', 'Block 1078', 'Block 2198', 'Block 3062', 'Block 6026', 'Block 2195', 'Block 2187', 'Block 2191', 'Block 2157', 'Block 2130', 'Block 3043', 'Block 5052', 'Block 6016', 'Block 6014', 'Block 2193', 'Block 2197', 'Block 7028', 'Block 6024', 'Block 2136', 'Block 2132', 'Block 1106', 'Block 5032', 'Block 1087', 'Block 2064', 'Block 2178', 'Block 2122', 'Block 7003', 'Block 1075', 'Block 4083', 'Block 2126', 'Block 1107', 'Block 6039', 'Block 7007', 'Block 3054', 'Block 4074', 'Block 2088', 'Block 2089', 'Block 2172', 'Block 6006', 'Block 2233', 'Block 4045', 'Block 4108', 'Block 2065', 'Block 7010', 'Block 1056', 'Block 4070', 'Block 1072', 'Block 4072', 'Block 7005', 'Block 7008', 'Block 2066', 'Block 4064', 'Block 7030', 'Block 7000', 'Block 1065', 'Block 1071', 'Block 2201', 'Block 3061', 'Block 4044', 'Block 7009', 'Block 4078', 'Block 6022', 'Block 3044', 'Block 2202', 'Block 1059', 'Block 7020', 'Block 2219', 'Block 2096', 'Block 2224', 'Block 2221', 'Block 7011', 'Block 6038', 'Block 1083', 'Block 7002', 'Block 4075', 'Block 5048', 'Block 5053', 'Block 4087', 'Block 2074', 'Block 6023', 'Block 4065', 'Block 4085', 'Block 3072', 'Block 2101', 'Block 4076', 'Block 2206', 'Block 2210', 'Block 1105', 'Block 7019', 'Block 5038', 'Block 7025', 'Block 5031', 'Block 2123', 'Block 7004', 'Block 3056', 'Block 7022', 'Block 1076', 'Block 6034', 'Block 2114', 'Block 2110', 'Block 2095', 'Block 5037', 'Block 7018', 'Block 3051', 'Block 2213', 'Block 2180', 'Block 2165', 'Block 2166', 'Block 2139', 'Block 4057', 'Block 2181', 'Block 2152', 'Block 2151', 'Block 4062', 'Block 7027', 'Block 1069', 'Block 2214', 'Block 2112', 'Block 2079', 'Block 4073', 'Block 2116', 'Block 4086', 'Block 4081', 'Block 1097', 'Block 1098', 'Block 4084', 'Block 2107', 'Block 2121', 'Block 2171', 'Block 5040', 'Block 2083', 'Block 2125', 'Block 6035', 'Block 6027', 'Block 7021', 'Block 1103', 'Block 7006', 'Block 3063', 'Block 2194', 'Block 2183', 'Block 2175', 'Block 2203', 'Block 1092', 'Block 4104', 'Block 6033', 'Block 2077', 'Block 2225', 'Block 2208', 'Block 4056', 'Block 2186', 'Block 2190', 'Block 2143', 'Block 1081', 'Block 4060', 'Block 5054', 'Block 5043', 'Block 5046', 'Block 7023', 'Block 7024', 'Block 6040', 'Block 2232', 'Block 2184', 'Block 2207', 'Block 2176', 'Block 2170', 'Block 2156', 'Block 2173', 'Block 2141', 'Block 4068', 'Block 5041', 'Block 7026', 'Block 5042', 'Block 2196', 'Block 2118', 'Block 2227', 'Block 4071', 'Block 1094', 'Block 2226', 'Block 1095', 'Block 1090', 'Block 2100', 'Block 2127', 'Block 2120', 'Block 4061', 'Block 4079', 'Block 5058', 'Block 2137', 'Block 5055', 'Block 5045', 'Block 5057', 'Block 1099', 'Block 2220', 'Block 2216', 'Block 2211', 'Block 3071', 'Block 2185', 'Block 2164', 'Block 5056', 'Block 4089', 'Block 2104', 'Block 4093', 'Block 2205', 'Block 2138', 'Block 2134', 'Block 2177', 'Block 7029', 'Block 4098', 'Block 6030', 'Block 2223', 'Block 2182', 'Block 2140', 'Block 2231', 'Block 2229', 'Block 3058', 'Block 2204', 'Block 4088', 'Block 4099', 'Block 4101', 'Block 2169', 'Block 3059', 'Block 4092', 'Block 2128', 'Block 1108', 'Block 5050', 'Block 5049', 'Block 3057', 'Block 2222', 'Block 2162', 'Block 4091', 'Block 4077', 'Block 4080', 'Block 2189', 'Block 2150', 'Block 2160', 'Block 2158', 'Block 5051', 'Block 6037', 'Block 2129', 'Block 1104', 'Block 4100', 'Block 4095', 'Block 5044', 'Block 2174', 'Block 2153', 'Block 2147', 'Block 4090', 'Block 2167', 'Block 2144', 'Block 2145', 'Block 2200', 'Block 2199', 'Block 2146', 'Block 2148', 'Block 2142', 'Block 4103', 'Block 4094', 'Block 2159', 'Block 4105', 'Block 4106', 'Block 3060', 'Block 4102', 'Block 4097', 'Block 2188', 'Block 2135', 'Block 4107', 'Block 5047', 'Block 2218'], dtype=object)
type(chicago_census_blocks)
geopandas.geodataframe.GeoDataFrame
cook_acs_df['Geography'].nunique()
3994
tmp_within = gpd.sjoin(chicago_census_blocks, cook_acs_df, op="within")
tmp_within.head()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-43-20c187e1c999> in <module>() ----> 1 tmp_within = gpd.sjoin(chicago_census_blocks, cook_acs_df, op="within") 2 tmp_within.head() ~\Anaconda3\envs\geo36\lib\site-packages\geopandas\tools\sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix) 41 (op, allowed_ops)) 42 ---> 43 if left_df.crs != right_df.crs: 44 warn('CRS of frames being joined does not match!') 45 ~\Anaconda3\envs\geo36\lib\site-packages\pandas\core\generic.py in __getattr__(self, name) 4374 if self._info_axis._can_hold_identifiers_and_holds_name(name): 4375 return self[name] -> 4376 return object.__getattribute__(self, name) 4377 4378 def __setattr__(self, name, value): AttributeError: 'DataFrame' object has no attribute 'crs'
len("170316903002010")
15
len("170310102012")
12
len("6903002010")
10
"6903002010" in "170316903002010"
True
cook_acs_df[cook_acs_df['Id2'].str.contains("6903002010")]
Id | Id2 | Geography | Total: | Population of one race: | Population of one race: - White alone | Population of one race: - Black or African American alone | Population of one race: - American Indian and Alaska Native alone | Population of one race: - Asian alone | Population of one race: - Native Hawaiian and Other Pacific Islander alone | Population of one race: - Some Other Race alone | Two or More Races: | Two or More Races: - Population of two races: | Two or More Races: - Population of two races: - White; Black or African American | Two or More Races: - Population of two races: - White; American Indian and Alaska Native | Two or More Races: - Population of two races: - White; Asian | Two or More Races: - Population of two races: - White; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - White; Some Other Race | Two or More Races: - Population of two races: - Black or African American; American Indian and Alaska Native | Two or More Races: - Population of two races: - Black or African American; Asian | Two or More Races: - Population of two races: - Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Black or African American; Some Other Race | Two or More Races: - Population of two races: - American Indian and Alaska Native; Asian | Two or More Races: - Population of two races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of two races: - Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of two races: - Asian; Some Other Race | Two or More Races: - Population of two races: - Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: | Two or More Races: - Population of three races: - White; Black or African American; American Indian and Alaska Native | Two or More Races: - Population of three races: - White; Black or African American; Asian | Two or More Races: - Population of three races: - White; Black or African American; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Black or African American; Some Other Race | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - White; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - White; Asian; Some Other Race | Two or More Races: - Population of three races: - White; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - Black or African American; Asian; Some Other Race | Two or More Races: - Population of three races: - Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of three races: - American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of three races: - American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of three races: - Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Asian | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; American Indian and Alaska Native; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; Black or African American; Asian; Some Other Race | Two or More Races: - Population of four races: - White; Black or African American; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - White; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - White; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of four races: - Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of four races: - American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Asian; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; American Indian and Alaska Native; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; Black or African American; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - White; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of five races: - Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race | Two or More Races: - Population of six races: | Two or More Races: - Population of six races: - White; Black or African American; American Indian and Alaska Native; Asian; Native Hawaiian and Other Pacific Islander; Some Other Race |
---|
df[df['A'].str.contains("hello")]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-29-03c2d77a5e0c> in <module>() ----> 1 df["170316903002010".str.contains(df['A'])] NameError: name 'df' is not defined