Generate new columns by mapping existing data to common lookup tables.
The agate-lookup extension adds a lookup
method to agate's Table class.
Starting with a table that looks like this:
year | cost |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
We can map the year
column to its annual CPI index in one lookup call.
import agatelookup
agatelookup.patch()
join_year_cpi = table.lookup('year', 'cpi')
The return table will have now have a new column:
year | cost | cpi |
---|---|---|
|
|
152.383 |
|
|
160.525 |
|
|
156.858 |
|
|
184.000 |
|
|
207.344 |
|
|
195.267 |
A simple computation tacked on to this lookup can then get the 2015 equivalent values of each cost:
cpi_2015 = Decimal(216.909)
def cpi_adjust_2015(row):
return (row['cost'] * (cpi_2015 / row['cpi'])).quantize(Decimal('0.01'))
cost_2015 = join_year_cpi.compute([
('cost_2015', agate.Formula(agate.Number(), cpi_adjust_2015))
])
And the final table will look like this:
year | cost | cpi | cost_2015 |
---|---|---|---|
|
|
152.383 |
|
|
|
160.525 |
|
|
|
156.858 |
|
|
|
184.000 |
|
|
|
207.344 |
|
|
|
195.267 |
|