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.utils import set_log_level
set_log_level('debug')
In [2]:
from cartoframes.auth import set_default_credentials
set_default_credentials()
In [3]:
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[3]:
In [4]:
census_track_gdf = census_track_gdf.head(1)
In [5]:
from cartoframes.data.observatory import Catalog
Catalog().country('usa').category('demographics').geographies
Out[5]:
In [6]:
datasets = Catalog().country('usa').category('demographics').geography('usct_censustract_bc698c5a').datasets
datasets.to_dataframe()
Out[6]:
In [7]:
from cartoframes.data.observatory import Dataset
dataset = Dataset.get('acs_sociodemogr_d4b2cf03')
variables_df = dataset.variables
In [8]:
for variable in variables_df:
print(variable.agg_method, variable.db_type, variable.slug)
In [9]:
from cartoframes.data.observatory import Variable
v1 = Variable.get('no_car_2207f034') # SUM, FLOAT
v2 = Variable.get('poverty_4c2c9ac5') # AVG, FLOAT
v3 = Variable.get('geoidsl_52dffc46') # None, STRING
variables = [v1, v2, v3]
In [10]:
from cartoframes.data.observatory import Enrichment
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation=None
)
In [11]:
enriched_dataset_gdf.head(20)
Out[11]:
In [22]:
from cartoframes.data.observatory import Enrichment
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation=None,
filters={v1.id: "> 100", v2.id: "< 300"}
)
enriched_dataset_gdf.head(20)
Out[22]:
In [12]:
from cartoframes.data.observatory import Enrichment
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables
)
In [13]:
enriched_dataset_gdf.head(20)
Out[13]:
In [14]:
from cartoframes.data.observatory import Enrichment
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation='ARRAY_AGG'
)
In [15]:
enriched_dataset_gdf.head(20)
Out[15]:
In [16]:
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation={v1.id: ['SUM', 'AVG'], v2.id:'AVG'}
)
enriched_dataset_gdf.head(20)
Out[16]:
In [17]:
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation=None,
filters={v1.id: "> 0"}
)
enriched_dataset_gdf.head(20)
Out[17]:
In [18]:
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation=None,
filters={v1.id: ["> 0", "< 200"]}
)
enriched_dataset_gdf.head(20)
Out[18]:
In [19]:
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
filters={v1.id: ["> 0", "< 200"]}
)
enriched_dataset_gdf.head(20)
Out[19]:
In [20]:
enrichment = Enrichment()
enriched_dataset_gdf = enrichment.enrich_polygons(
census_track_gdf,
variables=variables,
aggregation={v1.id: ['SUM', 'AVG'], v2.id:'AVG'},
filters={v1.id: ["> 0", "< 200"]}
)
enriched_dataset_gdf.head(20)
Out[20]: