Skip to content

Commit

Permalink
Merge pull request #1421 from CartoDB/doc-updates
Browse files Browse the repository at this point in the history
small docs fixes
  • Loading branch information
Andy Eschbacher committed Dec 30, 2019
2 parents 9a4935d + 4e23ef0 commit a229c48
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions cartoframes/data/observatory/enrichment/enrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def enrich_points(self, dataframe, variables, geom_col=None, filters={}):
from cartoframes.auth import set_default_credentials
from cartoframes.data.observatory import Enrichment, Catalog
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
variables = catalog.country('usa').category('demographics').datasets[0].variables
Expand All @@ -81,9 +82,10 @@ def enrich_points(self, dataframe, variables, geom_col=None, filters={}):
from cartoframes.auth import set_default_credentials
from cartoframes.data.observatory import Enrichment, Catalog
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
all_variables = catalog.country('usa').category('demographics').datasets[0].variables
Expand All @@ -106,9 +108,10 @@ def enrich_points(self, dataframe, variables, geom_col=None, filters={}):
from cartoframes.auth import set_default_credentials
from cartoframes.data.observatory import Enrichment, Catalog
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
variable = catalog.country('usa').category('demographics').datasets[0].variables[0]
Expand Down Expand Up @@ -192,9 +195,10 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.auth import set_default_credentials
from cartoframes.data.observatory import Enrichment, Catalog
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
variable = catalog.country('usa').category('demographics').datasets[0].variables[0]
Expand All @@ -212,9 +216,10 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.auth import set_default_credentials
from cartoframes.data.observatory import Enrichment, Catalog
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
variables = catalog.country('usa').category('demographics').datasets[0].variables
Expand All @@ -231,9 +236,10 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.auth import set_default_credentials
from cartoframes.data.observatory import Enrichment, Catalog
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/csv/csv')
catalog = Catalog()
all_variables = catalog.country('usa').category('demographics').datasets[0].variables
Expand All @@ -256,9 +262,10 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.data.observatory import Enrichment, Catalog
from cartoframes.auth import set_default_credentials
# load local credentials file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
variable = catalog.country('usa').category('demographics').datasets[0].variables[0]
Expand All @@ -275,20 +282,24 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.data.observatory import Enrichment, Catalog
from cartoframes.auth import set_default_credentials
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
all_variables = catalog.country('usa').category('demographics').datasets[0].variables
variable1 = all_variables[0] // variable1.agg_method is 'AVG' but you want 'SUM'
variable2 = all_variables[1] // variable2.agg_method is 'AVG' and it is what you want
variable3 = all_variables[2] // variable3.agg_method is 'SUM' but you want 'AVG'
variable1 = all_variables[0] # variable1.agg_method is 'AVG' but you want 'SUM'
variable2 = all_variables[1] # variable2.agg_method is 'AVG' but you want 'SUM'
variable3 = all_variables[2] # variable3.agg_method is 'SUM' and you want to keep it
variables = [variable1, variable2, variable3]
enrichment = Enrichment()
cdf_enrich = enrichment.enrich_polygons(df, variables, aggregation='SUM')
# override default aggregation of all variables with `SUM`
cdf_enrich = enrichment.enrich_polygons(df, variables,
aggregation='SUM')
Enrich a polygons dataframe overwriting some of the variables aggregation methods:
Expand All @@ -298,18 +309,21 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.data.observatory import Enrichment, Catalog
from cartoframes.auth import set_default_credentials
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
all_variables = catalog.country('usa').category('demographics').datasets[0].variables
variable1 = all_variables[0] // variable1.agg_method is 'AVG' but you want 'SUM'
variable2 = all_variables[1] // variable2.agg_method is 'AVG' and it is what you want
variable3 = all_variables[2] // variable3.agg_method is 'SUM' but you want 'AVG'
# aggregation methods can be overridden
variable1 = all_variables[0] # variable1.agg_method is 'AVG' but you want 'SUM'
variable2 = all_variables[1] # variable2.agg_method is 'AVG' and want to keep it that way
variable3 = all_variables[2] # variable3.agg_method is 'SUM' but you want 'AVG'
variables = [variable1, variable2, variable3]
# override default aggregations
aggregation = {
variable1.id: 'SUM',
variable3.id: 'AVG'
Expand All @@ -327,9 +341,10 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
from cartoframes.data.observatory import Enrichment, Catalog
from cartoframes.auth import set_default_credentials
# load credentials from local file
set_default_credentials('creds.json')
df = pandas.read_csv('...')
df = pandas.read_csv('path/to/local/csv')
catalog = Catalog()
all_variables = catalog.country('usa').category('demographics').datasets[0].variables
Expand All @@ -340,6 +355,7 @@ def enrich_polygons(self, dataframe, variables, geom_col=None, filters={}, aggre
variables = [variable1, variable2, variable3]
enrichment = Enrichment()
# disable aggregation for all variables
cdf_enrich = enrichment.enrich_polygons(df, variables, aggregation=None)
The next example uses filters to calculate the `SUM` of car-free households
Expand Down

0 comments on commit a229c48

Please sign in to comment.