This example illustrates how to enrich polygons that are in a dataset with variables from CARTO's Data Observatory.
Note: You'll need CARTO Account credentials to reproduce this example.
In [1]:
from cartoframes.auth import set_default_credentials
set_default_credentials('creds.json')
In [2]:
from geopandas import read_file
census_track = 'http://libs.cartocdn.com/cartoframes/files/census_track.geojson'
census_track_gdf = read_file(census_track)
census_track_gdf.head(3)
Out[2]:
OBJECTID
FULLTRACTID
TRACTID
geometry
0
1
51013102901
102901
POLYGON ((-77.09099 38.84516, -77.08957 38.844...
1
2
51013103000
103000
POLYGON ((-77.08558 38.82992, -77.08625 38.828...
2
3
51013102902
102902
POLYGON ((-77.09520 38.84499, -77.09442 38.844...
In [3]:
from cartoframes.data.observatory import Catalog
Catalog().country('usa').category('demographics').geographies
Out[3]:
[<Geography.get('mbi_blockgroups_535aed6d')>,
<Geography.get('mbi_counties_46ea8aaa')>,
<Geography.get('mbi_county_subd_ba170144')>,
<Geography.get('mbi_pc_5_digit_19e769c1')>,
<Geography.get('cdb_blockgroup_7753dd51')>,
<Geography.get('cdb_cbsa_d1b91d3b')>,
<Geography.get('cdb_censustract_af861cba')>,
<Geography.get('cdb_congression_478295fd')>,
<Geography.get('cdb_county_767e79f0')>,
<Geography.get('cdb_county_8cf054d')>,
<Geography.get('cdb_place_93d54d1e')>,
<Geography.get('cdb_puma_56bbc2e')>,
<Geography.get('cdb_schooldistr_eb48e7bc')>,
<Geography.get('cdb_schooldistr_18547e3f')>,
<Geography.get('cdb_schooldistr_d9ca1a26')>,
<Geography.get('cdb_state_cd83b434')>,
<Geography.get('cdb_zcta5_f4043497')>]
In [4]:
datasets = Catalog().country('usa').category('demographics').geography('cdb_censustract_af861cba').datasets
datasets.to_dataframe()
Out[4]:
slug
name
description
category_id
country_id
data_source_id
provider_id
geography_name
geography_description
temporal_aggregation
time_coverage
update_frequency
is_public_data
lang
version
category_name
provider_name
geography_id
id
0
acs_sociodemogr_cfeb0968
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2009-01-01, 2014-01-01)
yearly
True
eng
20092013
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
1
acs_sociodemogr_858c104e
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2008-01-01, 2013-01-01)
yearly
True
eng
20082012
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
2
acs_sociodemogr_d4b2cf03
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2006-01-01, 2011-01-01)
yearly
True
eng
20062010
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
3
acs_sociodemogr_496a0675
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2013-01-01, 2018-01-01)
yearly
True
eng
20132017
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
4
acs_sociodemogr_30d1f53
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2012-01-01, 2017-01-01)
yearly
True
eng
20122016
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
5
acs_sociodemogr_dda43439
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2011-01-01, 2016-01-01)
yearly
True
eng
20112015
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
6
acs_sociodemogr_97c32d1f
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2010-01-01, 2015-01-01)
yearly
True
eng
20102014
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
7
acs_sociodemogr_9ed5d625
Sociodemographics - United States of America (...
The American Community Survey (ACS) is an ongo...
demographics
usa
sociodemographics
usa_acs
Census Tract - United States of America
Shoreline clipped TIGER/Line boundaries. More ...
5yrs
[2007-01-01, 2012-01-01)
yearly
True
eng
20072011
Demographics
American Community Survey
carto-do-public-data.carto.geography_usa_censu...
carto-do-public-data.usa_acs.demographics_soci...
In [5]:
from cartoframes.data.observatory import Dataset
dataset = Dataset.get('acs_sociodemogr_d4b2cf03')
variables_df = dataset.variables.to_dataframe()
variables_df[variables_df['description'].str.contains('car', case=False, na=False)]
Out[5]:
slug
name
description
db_type
agg_method
column_name
variable_group_id
dataset_id
id
79
median_income_57be5af4
Median Income
Median Household Income in the past 12 Months....
FLOAT
AVG
median_income
None
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
165
no_car_2207f034
no_car
Workers age 16 and over with no vehicle. All p...
FLOAT
SUM
no_car
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
166
no_cars_3a983c4e
no_cars
Car-free households. The number of households ...
FLOAT
SUM
no_cars
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
167
one_car_13b3a60b
one_car
One car households. The number of households w...
FLOAT
SUM
one_car
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
168
two_cars_fec37223
two_cars
Two car households. The number of households w...
FLOAT
SUM
two_cars
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
169
three_cars_32e541e7
three_cars
Three car households. The number of households...
FLOAT
SUM
three_cars
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
170
four_more_cars_297e8a8a
four_more_cars
Four car households. The number of households ...
FLOAT
SUM
four_more_cars
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
174
commuters_by_ca_120481e3
commuters_by_car_truck_van
Commuters by Car, Truck, or Van. The number of...
FLOAT
SUM
commuters_by_car_truck_van
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
175
commuters_by_ca_59febe6f
commuters_by_carpool
Commuters by Carpool. The number of workers ag...
FLOAT
SUM
commuters_by_carpool
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
177
commuters_drove_acf77a1
commuters_drove_alone
Commuters who drove alone. The number of worke...
FLOAT
SUM
commuters_drove_alone
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
197
employed_educat_958fdcfe
employed_education_health_social
Workers employed in firms in educational servi...
FLOAT
SUM
employed_education_health_social
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
201
employed_other__a5acf594
employed_other_services_not_public_admin
Workers employed in firms in other services ex...
FLOAT
SUM
employed_other_services_not_public_admin
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
205
employed_transp_66aedbfd
employed_transportation_warehousing_utilities
Workers employed in firms in transportation, w...
FLOAT
SUM
employed_transportation_warehousing_utilities
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
carto-do-public-data.usa_acs.demographics_soci...
In [6]:
from cartoframes.data.observatory import Variable
variable = Variable.get('no_car_2207f034')
variable.to_dict()
Out[6]:
{'slug': 'no_car_2207f034',
'name': 'no_car',
'description': 'Workers age 16 and over with no vehicle. All people in a geographic area over the age of 16 who do not own a car.',
'db_type': 'FLOAT',
'agg_method': 'SUM',
'column_name': 'no_car',
'variable_group_id': 'carto-do-public-data.usa_acs.demographics_sociodemographics_usa_censustract_2015_5yrs_20062010.car_ownership',
'dataset_id': 'carto-do-public-data.usa_acs.demographics_sociodemographics_usa_censustract_2015_5yrs_20062010',
'id': 'carto-do-public-data.usa_acs.demographics_sociodemographics_usa_censustract_2015_5yrs_20062010.no_car'}
In [7]:
from cartoframes.data.observatory import Enrichment
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=[variable],
aggregation='SUM'
)
In [8]:
enriched_dataset_gdf.head(3)
Out[8]:
OBJECTID
FULLTRACTID
TRACTID
geometry
no_car
0
1
51013102901
102901
POLYGON ((-77.09099 38.84516, -77.08957 38.844...
0.657821
1
2
51013103000
103000
POLYGON ((-77.08558 38.82992, -77.08625 38.828...
87.516848
2
3
51013102902
102902
POLYGON ((-77.09520 38.84499, -77.09442 38.844...
123.512837
In [9]:
from cartoframes.viz import Layer, color_continuous_style
Layer(enriched_dataset_gdf, color_continuous_style('no_car'))
Out[9]:
:
StackTrace
">
Content source: CartoDB/cartoframes
Similar notebooks: