CARTOframes

A Python package for integrating CARTO maps, analysis, and data services into data science workflows.

CARTOframes v1.0.4 includes breaking changes from betas and 0.10 version, check the migration guide to learn how to update your code.

Data enrichment

Introduction

We define enrichment as the process of augmenting your data with new variables by means of a spatial join between your data and a Dataset aggregated at a given spatial resolution in the CARTO Data Observatory, or in other words:

Enrichment is the process of adding variables to a geometry, which we call the target, (point, line, polygon…) from a spatial (polygon) dataset, which we call the source

We recommend you check out the CARTOframes quickstart since this guide uses some of the generated DataFrames as well as the Data Discovery guide to learn about exploring the Data Observatory catalog to find variables of interest for your analyses.

Choose variables to enrich from the Data Observatory catalog

Let’s follow up with the Data Discovery guide, where we subscribed to the AGS demographics dataset and listed the variables available to enrich our own data.

1
2
3
from cartoframes.auth import set_default_credentials

set_default_credentials('creds.json')
1
2
from cartoframes.data.observatory import Catalog, Dataset, Variable, Geography
Catalog().subscriptions().datasets
1
2
3
4
5
[<Dataset.get('ags_businesscou_df363a87')>,
 <Dataset.get('ags_crimerisk_e9cfa4d4')>,
 <Dataset.get('ags_retailpoten_aaf25a8c')>,
 <Dataset.get('ags_sociodemogr_f510a947')>,
 <Dataset.get('ags_sociodemogr_a7e14220')>]
1
2
3
dataset = Dataset.get('ags_sociodemogr_f510a947')
variables = dataset.variables
variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[<Variable.get('BLOCKGROUP_108673f9')> #'Geographic Identifier',
 <Variable.get('POPCY_5e23b8f4')> #'Population (2019A)',
 <Variable.get('POPCYGRP_55c4a2e5')> #'Population in Group Quarters (2019A)',
 <Variable.get('POPCYGRPI_d37c4ec')> #'Institutional Group Quarters Population (2019A)',
 <Variable.get('AGECY0004_a67ddb4f')> #'Population age 0-4 (2019A)',
 <Variable.get('AGECY0509_de076519')> #'Population age 5-9 (2019A)',
 <Variable.get('AGECY1014_7da8d6b')> #'Population age 10-14 (2019A)',
 <Variable.get('AGECY1519_7fa0333d')> #'Population age 15-19 (2019A)',
 <Variable.get('AGECY2024_3e427146')> #'Population age 20-24 (2019A)',
 <Variable.get('AGECY2529_4638cf10')> #'Population age 25-29 (2019A)',
 <Variable.get('AGECY3034_9fe52762')> #'Population age 30-34 (2019A)',
 <Variable.get('AGECY3539_e79f9934')> #'Population age 35-39 (2019A)',
 <Variable.get('AGECY4044_4d73891c')> #'Population age 40-44 (2019A)',
 <Variable.get('AGECY4549_3509374a')> #'Population age 45-49 (2019A)',
 <Variable.get('AGECY5054_ecd4df38')> #'Population age 50-54 (2019A)',
 <Variable.get('AGECY5559_94ae616e')> #'Population age 55-59 (2019A)',
 <Variable.get('AGECY6064_d54c2315')> #'Population age 60-64 (2019A)',
 <Variable.get('AGECY6569_ad369d43')> #'Population age 65-69 (2019A)',
 <Variable.get('AGECY7074_74eb7531')> #'Population age 70-74 (2019A)',
 <Variable.get('AGECY7579_c91cb67')> #'Population age 75-79 (2019A)',
 <Variable.get('AGECY8084_ab1079a8')> #'Population age 80-84 (2019A)',
 <Variable.get('AGECYGT85_a0959a08')> #'Population age 85+ (2019A)',
 <Variable.get('AGECYMED_97ef9b22')> #'Median Age (2019A)',
 <Variable.get('SEXCYMAL_eb11e02e')> #'Population male (2019A)',
 <Variable.get('SEXCYFEM_f42ffa5d')> #'Population female (2019A)',
 <Variable.get('RCHCYWHNHS_94a9e0a1')> #'Non Hispanic White (2019A)',
 <Variable.get('RCHCYBLNHS_b3cb6f04')> #'Non Hispanic Black (2019A)',
 <Variable.get('RCHCYAMNHS_4cd772b1')> #'Non Hispanic American Indian (2019A)',
 <Variable.get('RCHCYASNHS_fc11521d')> #'Non Hispanic Asian (2019A)',
 <Variable.get('RCHCYHANHS_b11af78')> #'Non Hispanic Hawaiian/Pacific Islander (2019A)',
 <Variable.get('RCHCYOTNHS_def6d4c5')> #'Non Hispanic Other Race (2019A)',
 <Variable.get('RCHCYMUNHS_1c8ae0c0')> #'Non Hispanic Multiple Race (2019A)',
 <Variable.get('HISCYHISP_eafe905b')> #'Population Hispanic (2019A)',
 <Variable.get('MARCYNEVER_ce87ae9c')> #'Never Married (2019A)',
 <Variable.get('MARCYMARR_1b2334f2')> #'Now Married (2019A)',
 <Variable.get('MARCYSEP_b121d373')> #'Separated (2019A)',
 <Variable.get('MARCYWIDOW_7c868fcc')> #'Widowed (2019A)',
 <Variable.get('MARCYDIVOR_340ee10f')> #'Divorced (2019A)',
 <Variable.get('AGECYGT15_71572141')> #'Population Age 15+ (2019A)',
 <Variable.get('EDUCYLTGR9_cd6034a5')> #'Pop 25+ less than 9th grade (2019A)',
 <Variable.get('EDUCYSHSCH_5aebb5c7')> #'Pop 25+ 9th-12th grade no diploma (2019A)',
 <Variable.get('EDUCYHSCH_ab7bfb46')> #'Pop 25+ HS graduate (2019A)',
 <Variable.get('EDUCYSCOLL_1823b004')> #'Pop 25+ college no diploma (2019A)',
 <Variable.get('EDUCYASSOC_fcb4373f')> #'Pop 25+ Associate degree (2019A)',
 <Variable.get('EDUCYBACH_db646c3c')> #'Pop 25+ Bachelors degree (2019A)',
 <Variable.get('EDUCYGRAD_c95aaf8e')> #'Pop 25+ graduate or prof school degree (2019A)',
 <Variable.get('AGECYGT25_5a7a7282')> #'Population Age 25+ (2019A)',
 <Variable.get('HHDCY_884b57a2')> #'Households (2019A)',
 <Variable.get('HHDCYFAM_a451b104')> #'Family Households (2019A)',
 <Variable.get('HHSCYMCFCH_9d720b9d')> #'Families married couple w children (2019A)',
 <Variable.get('HHSCYLPMCH_eeeb35bd')> #'Families male no wife w children (2019A)',
 <Variable.get('HHSCYLPFCH_e2beda5c')> #'Families female no husband children (2019A)',
 <Variable.get('HHDCYAVESZ_f206a443')> #'Average Household Size (2019A)',
 <Variable.get('HHDCYMEDAG_6f6ac70e')> #'Median Age of Householder (2019A)',
 <Variable.get('VPHCYNONE_3b864015')> #'Households: No Vehicle Available (2019A)',
 <Variable.get('VPHCY1_98166634')> #'Households: One Vehicle Available (2019A)',
 <Variable.get('VPHCYGT1_815731fb')> #'Households: Two or More Vehicles Available (2019A)',
 <Variable.get('INCCYPCAP_70509bba')> #'Per capita income (2019A)',
 <Variable.get('INCCYAVEHH_3e94053c')> #'Average household Income (2019A)',
 <Variable.get('INCCYMEDHH_b80a7a7b')> #'Median household income (2019A)',
 <Variable.get('INCCYMEDFA_5f55ef51')> #'Median family income (2019A)',
 <Variable.get('HINCYLT10_6d12a25c')> #'Household Income < $10000 (2019A)',
 <Variable.get('HINCY1015_cbf34d6e')> #'Household Income $10000-$14999 (2019A)',
 <Variable.get('HINCY1520_967f28c9')> #'Household Income $15000-$19999 (2019A)',
 <Variable.get('HINCY2025_f26bb143')> #'Household Income $20000-$24999 (2019A)',
 <Variable.get('HINCY2530_9dd1b666')> #'Household Income $25000-$29999 (2019A)',
 <Variable.get('HINCY3035_53cce767')> #'Household Income $30000-$34999 (2019A)',
 <Variable.get('HINCY3540_6a2c47c4')> #'Household Income $35000-$39999 (2019A)',
 <Variable.get('HINCY4045_815a4919')> #'Household Income $40000-$44999 (2019A)',
 <Variable.get('HINCY4550_eee04e3c')> #'Household Income $45000-$49999 (2019A)',
 <Variable.get('HINCY5060_7bbab871')> #'Household Income $50000-$59999 (2019A)',
 <Variable.get('HINCY6075_7ed251')> #'Household Income $60000-$74999 (2019A)',
 <Variable.get('HINCY75100_9bf391e4')> #'Household Income $75000-$99999 (2019A)',
 <Variable.get('HINCY10025_60f3684c')> #'Household Income $100000-$124999 (2019A)',
 <Variable.get('HINCY12550_f31a0064')> #'Household Income $125000-$149999 (2019A)',
 <Variable.get('HINCY15020_27476cf1')> #'Household Income $150000-$199999 (2019A)',
 <Variable.get('HINCYGT200_e3fd5f14')> #'Household Income > $200000 (2019A)',
 <Variable.get('HINCYMED24_24cfc536')> #'Median Household Income: Age < 25 (2019A)',
 <Variable.get('HINCYMED25_53c8f5a0')> #'Median Household Income: Age 25-34 (2019A)',
 <Variable.get('HINCYMED35_4ad3c4e1')> #'Median Household Income: Age 35-44 (2019A)',
 <Variable.get('HINCYMED45_5925226')> #'Median Household Income: Age 45-54 (2019A)',
 <Variable.get('HINCYMED55_1c896367')> #'Median Household Income: Age 55-64 (2019A)',
 <Variable.get('HINCYMED65_37a430a4')> #'Median Household Income: Age 65-74 (2019A)',
 <Variable.get('HINCYMED75_2ebf01e5')> #'Median Household Income: Age 75+ (2019A)',
 <Variable.get('LBFCYPOP16_55556a30')> #'Population Age 16+ (2019A)',
 <Variable.get('LBFCYARM_ad0316ac')> #'Pop 16+ in Armed Forces (2019A)',
 <Variable.get('LBFCYEMPL_15d111e5')> #'Pop 16+ civilian employed (2019A)',
 <Variable.get('LBFCYUNEM_73c2ea1')> #'Pop 16+ civilian unemployed (2019A)',
 <Variable.get('LBFCYNLF_e5ccb7c6')> #'Pop 16+ not in labor force (2019A)',
 <Variable.get('UNECYRATE_aa9101ff')> #'Unemployment Rate (2019A)',
 <Variable.get('LBFCYLBF_78cb4e26')> #'Population In Labor Force (2019A)',
 <Variable.get('LNIEXSPAN_8354c4b2')> #'SPANISH SPEAKING HOUSEHOLDS',
 <Variable.get('LNIEXISOL_ce3b81b2')> #'LINGUISTICALLY ISOLATED HOUSEHOLDS (NON-ENGLISH SP...',
 <Variable.get('HOOEXMED_2d287fcd')> #'Median Value of Owner Occupied Housing Units',
 <Variable.get('RNTEXMED_f35abc2')> #'Median Cash Rent',
 <Variable.get('HUSEX1DET_2fc97319')> #'UNITS IN STRUCTURE: 1 DETACHED',
 <Variable.get('HUSEXAPT_b98a71b9')> #'UNITS IN STRUCTURE: 20 OR MORE',
 <Variable.get('DWLCY_4bd2acd2')> #'Housing units (2019A)',
 <Variable.get('DWLCYVACNT_4bf1cbc5')> #'Housing units vacant (2019A)',
 <Variable.get('DWLCYRENT_3ad24aeb')> #'Occupied units renter (2019A)',
 <Variable.get('DWLCYOWNED_a5e86c89')> #'Occupied units owner (2019A)',
 <Variable.get('POPPY_3fccf966')> #'Population (2024A)',
 <Variable.get('HHDPY_e9a41630')> #'Households (2024A)',
 <Variable.get('DWLPY_2a3ded40')> #'Housing units (2024A)',
 <Variable.get('AGEPYMED_b0af7670')> #'Median Age (2024A)',
 <Variable.get('INCPYPCAP_f512eb8f')> #'Per capita income (2024A)',
 <Variable.get('INCPYAVEHH_68a2836f')> #'Average household Income (2024A)',
 <Variable.get('INCPYMEDHH_ee3cfc28')> #'Median household income (2024A)']

As we saw in the Data Discovery guide, the ags_sociodemogr_f510a947 dataset contains socio-demographic variables aggregated to the Census block group level.

Let’s try and find a variable for total population:

1
2
vdf = variables.to_dataframe()
vdf[vdf['name'].str.contains('pop', case=False, na=False)]
slug name description db_type agg_method column_name variable_group_id dataset_id id
1 POPCY_5e23b8f4 Total Population Population (2019A) INTEGER SUM POPCY None carto-do.ags.demographics_sociodemographics_us... carto-do.ags.demographics_sociodemographics_us...
2 POPCYGRP_55c4a2e5 POPCYGRP Population in Group Quarters (2019A) INTEGER SUM POPCYGRP None carto-do.ags.demographics_sociodemographics_us... carto-do.ags.demographics_sociodemographics_us...
3 POPCYGRPI_d37c4ec POPCYGRPI Institutional Group Quarters Population (2019A) INTEGER SUM POPCYGRPI None carto-do.ags.demographics_sociodemographics_us... carto-do.ags.demographics_sociodemographics_us...
84 LBFCYPOP16_55556a30 LBFCYPOP16 Population Age 16+ (2019A) INTEGER SUM LBFCYPOP16 carto-do.ags.demographics_sociodemographics_us... carto-do.ags.demographics_sociodemographics_us... carto-do.ags.demographics_sociodemographics_us...
101 POPPY_3fccf966 Total population Population (2024A) FLOAT SUM POPPY None carto-do.ags.demographics_sociodemographics_us... carto-do.ags.demographics_sociodemographics_us...

We can store the variable instance we need by searching the Catalog by its slug, in this case POPCY_5e23b8f4:

1
2
variable = Variable.get('POPCY_5e23b8f4')
variable.to_dict()
1
2
3
4
5
6
7
8
9
{'slug': 'POPCY_5e23b8f4',
 'name': 'Total Population',
 'description': 'Population (2019A)',
 'db_type': 'INTEGER',
 'agg_method': 'SUM',
 'column_name': 'POPCY',
 'variable_group_id': None,
 'dataset_id': 'carto-do.ags.demographics_sociodemographics_usa_blockgroup_2015_yearly_2019',
 'id': 'carto-do.ags.demographics_sociodemographics_usa_blockgroup_2015_yearly_2019.POPCY'}

The POPCY variable contains the SUM of the population for blockgroup for the year 2019. Let’s enrich our stores DataFrame with that variable.

Enrich a points DataFrame

In the CARTOframes Quickstart you learned how to load your own data (in this case Starbucks stores) and geocode the addresses to coordinates for further analysis.

Let’s start by loading those geocoded Starbucks stores:

1
2
3
from geopandas import read_file
stores_gdf = read_file('http://libs.cartocdn.com/cartoframes/files/starbucks_brooklyn_geocoded.geojson')
stores_gdf.head(5)
cartodb_id field_1 name address revenue geometry
0 1 0 Franklin Ave & Eastern Pkwy 341 Eastern Pkwy,Brooklyn, NY 11238 1321040.772 POINT (-73.95901 40.67109)
1 2 1 607 Brighton Beach Ave 607 Brighton Beach Avenue,Brooklyn, NY 11235 1268080.418 POINT (-73.96122 40.57796)
2 3 2 65th St & 18th Ave 6423 18th Avenue,Brooklyn, NY 11204 1248133.699 POINT (-73.98976 40.61912)
3 4 3 Bay Ridge Pkwy & 3rd Ave 7419 3rd Avenue,Brooklyn, NY 11209 1185702.676 POINT (-74.02744 40.63152)
4 5 4 Caesar's Bay Shopping Center 8973 Bay Parkway,Brooklyn, NY 11214 1148427.411 POINT (-74.00098 40.59321)

Note: Alternatively, you can load data in any geospatial format supported by GeoPandas or CARTO.

As we can see, for each store we have its name, address, the total revenue by year and a geometry column indicating the location of the store. This is important because for the enrichment service to work, we need a DataFrame with a geometry column encoded as a shapely object.

We can now create a new Enrichment instance, and since the stores_gdf dataset represents store locations (points), we can use the enrich_points function passing as arguments, the stores DataFrame and a list of Variables (that we have a valid subscription from the Data Observatory catalog for).

In this case we are only enriching one variable (the total population), but we could enrich a list of them.

1
2
3
from cartoframes.data.observatory import Enrichment
enriched_stores_gdf = Enrichment().enrich_points(stores_gdf, [variable])
enriched_stores_gdf.head(5)
cartodb_id field_1 name address revenue geometry POPCY do_area
0 1 0 Franklin Ave & Eastern Pkwy 341 Eastern Pkwy,Brooklyn, NY 11238 1321040.772 POINT (-73.95901 40.67109) 2215 59840.196748
1 2 1 607 Brighton Beach Ave 607 Brighton Beach Avenue,Brooklyn, NY 11235 1268080.418 POINT (-73.96122 40.57796) 1831 60150.637177
2 3 2 65th St & 18th Ave 6423 18th Avenue,Brooklyn, NY 11204 1248133.699 POINT (-73.98976 40.61912) 745 38950.618828
3 4 3 Bay Ridge Pkwy & 3rd Ave 7419 3rd Avenue,Brooklyn, NY 11209 1185702.676 POINT (-74.02744 40.63152) 1174 57353.293114
4 5 4 Caesar's Bay Shopping Center 8973 Bay Parkway,Brooklyn, NY 11214 1148427.411 POINT (-74.00098 40.59321) 2289 188379.242624

Once the enrichment finishes, there is a new column in our DataFrame called POPCY with population projected for the year 2019, from the US Census block group which contains each one of our Starbucks stores.

All this information, is available in the ags_sociodemogr_e92b1637 metadata. Let’s take a look:

1
dataset.to_dict()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{'slug': 'ags_sociodemogr_f510a947',
 'name': 'Sociodemographics - United States of America (Census Block Group)',
 'description': 'Census and ACS sociodemographic data estimated for the current year and data projected to five years. Projected fields are general aggregates (total population, total households, median age, avg income etc.)',
 'category_id': 'demographics',
 'country_id': 'usa',
 'data_source_id': 'sociodemographics',
 'provider_id': 'ags',
 'geography_name': 'Census Block Group - United States of America',
 'geography_description': 'Shoreline clipped TIGER/Line boundaries. More info: https://carto.com/blog/tiger-shoreline-clip/',
 'temporal_aggregation': 'yearly',
 'time_coverage': '[2019-01-01, 2020-01-01)',
 'update_frequency': 'yearly',
 'is_public_data': False,
 'lang': 'eng',
 'version': '2019',
 'category_name': 'Demographics',
 'provider_name': 'Applied Geographic Solutions',
 'geography_id': 'carto-do-public-data.carto.geography_usa_blockgroup_2015',
 'id': 'carto-do.ags.demographics_sociodemographics_usa_blockgroup_2015_yearly_2019'}

Enrich a polygon DataFrame

Next, let’s do a second enrichment, but this time using a DataFrame with areas of influence calculated using the CARTOframes isochrones service to obtain the polygon around each store that covers the area within an 8, 17 and 25 minute walk.

1
2
aoi_gdf = read_file('http://libs.cartocdn.com/cartoframes/files/starbucks_brooklyn_isolines.geojson')
aoi_gdf.head(5)
data_range lower_data_range range_label geometry
0 500 0 8 min. MULTIPOLYGON (((-73.95959 40.67571, -73.95971 ...
1 1000 500 17 min. POLYGON ((-73.95988 40.68110, -73.95863 40.681...
2 1500 1000 25 min. POLYGON ((-73.95986 40.68815, -73.95711 40.688...
3 500 0 8 min. MULTIPOLYGON (((-73.96185 40.58321, -73.96231 ...
4 1000 500 17 min. MULTIPOLYGON (((-73.96684 40.57483, -73.96830 ...

In this case we have a DataFrame which, for each index in the stores_gdf, contains a polygon of the areas of influence around each store at 8, 17 and 25 minute walking intervals. Again the geometry is encoded as a shapely object.

In this case, the Enrichment service provides an enrich_polygons function, which in its basic version, works in the same way as the enrich_points function. It just needs a DataFrame with polygon geometries and a list of variables to enrich:

1
2
3
from cartoframes.data.observatory import Enrichment
enriched_aoi_gdf = Enrichment().enrich_polygons(aoi_gdf, [variable])
enriched_aoi_gdf.head(5)
data_range lower_data_range range_label geometry POPCY
0 500 0 8 min. MULTIPOLYGON (((-73.95959 40.67571, -73.95971 ... 21112.458335
1 1000 500 17 min. POLYGON ((-73.95988 40.68110, -73.95863 40.681... 60157.083956
2 1500 1000 25 min. POLYGON ((-73.95986 40.68815, -73.95711 40.688... 110657.471670
3 500 0 8 min. MULTIPOLYGON (((-73.96185 40.58321, -73.96231 ... 23505.104589
4 1000 500 17 min. MULTIPOLYGON (((-73.96684 40.57483, -73.96830 ... 29781.046917

We now have a new column in our areas of influence DataFrame, SUM_POPCY which represents the SUM of total population in the Census block groups that instersect with each polygon in our DataFrame.

How enrichment works

Let’s take a deeper look into what happens under the hood when you execute a polygon enrichment.

Imagine we have polygons representing municipalities, in blue, each of which have a population attribute, and we want to find out the population inside the green circle.

Enrichment Schema

We don’t know how the population is distributed inside these municipalities. They are probably concentrated in cities somewhere, but, since we don’t know where they are, our best guess is to assume that the population is evenly distributed in the municipality (i.e. every point inside the municipality has the same population density).

Population is an extensive property (it grows with area), so we can subset it (a region inside the municipality will always have a smaller population than the whole municipality), and also aggregate it by summing.

In this case, we’d calculate the population inside each part of the circle that intersects with a municipality.

Default aggregation methods

In the Data Observatory, we suggest a default aggregation method for certain fields. However, some fields don’t have a clear best method, and some just can’t be aggregated. In these cases, we leave the agg_method field blank and let the user choose the method that best fits their needs.

Conclusion

In this guide you’ve seen how to use CARTOframes in conjunction with the Data Observatory to enrich a Starbucks dataset with a new population variable for the use case of revenue prediction analysis by:

  • Choosing the total population variable from the Data Observatory catalog
  • Calculating the sum of total population for each store
  • Calculating the sum of total population around the walking areas of influence around each store

In addition, you were introduced to some more advanced concepts and further explanation of how the enrichment itself works.