Ingredient: Using formattersΒΆ
This example shows a shelf with formatters.
[4]:
from examples_base import *
shelf = Shelf({
'state': Dimension(Census.state),
'age': WtdAvgMetric(Census.age, Census.pop2000),
'gender': Dimension(Census.gender),
'population': Metric(func.sum(Census.pop2000), formatters=[
lambda value: int(round(value, -6) / 1000000)
])
})
recipe = Recipe(shelf=shelf, session=oven.Session())\
.dimensions('gender').metrics('population')
recipe.dataset.df
[4]:
gender | population_raw | gender_id | population | |
---|---|---|---|---|
0 | F | 143534804 | F | 144 |
1 | M | 137392517 | M | 137 |
Formatters are python code that runs after the row data is retrieved from the database. The original value is available as ingredient_raw
.