#!/usr/bin/env python # coding: utf-8 # ## Enrich Polygons from a Dataset # # 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](https://carto.com/signup) 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) # In[3]: from cartoframes.data.observatory import Catalog Catalog().country('usa').category('demographics').geographies # In[4]: datasets = Catalog().country('usa').category('demographics').geography('cdb_censustract_af861cba').datasets datasets.to_dataframe() # 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)] # In[6]: from cartoframes.data.observatory import Variable variable = Variable.get('no_car_2207f034') variable.to_dict() # 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) # In[9]: from cartoframes.viz import Layer, color_continuous_style Layer(enriched_dataset_gdf, color_continuous_style('no_car'))