You are correct that the lookup transform only finds the first matching index - it's a one-sided join, not a multi-join. If you want to join multiple data entries per key, you'll have to use a dataset with multiple columns.
For your data, you can produce such a dataset using the pandas pivot method, and then within Altair you can undo this operation after the lookup by using the fold transform
For your data it might look something like this:
import altair as alt
import pandas as pd
from vega_datasets import data
us_counties = alt.topo_feature(data.us_10m.url, 'counties')
fdf = pd.read_csv('https://raw.githubusercontent.com/sdasara95/Opioid-Crisis/master/sample_data.csv')
fdf['year'] = fdf['year'].astype(str)
fdf = fdf.pivot(index='fips', columns='year', values='Pill_per_pop').reset_index()
columns = [str(year) for year in range(2006, 2013)]
slider = alt.binding_range(min=2006, max=2012, step=1)
select_year = alt.selection_single(name="year", fields=['year'],
bind=slider, init={'year': 2006})
alt.Chart(us_counties).mark_geoshape(
stroke='black',
strokeWidth=0.05
).project(
type='albersUsa'
).transform_lookup(
lookup='id',
from_=alt.LookupData(fdf, 'fips', columns)
).transform_fold(
columns, as_=['year', 'Pill_per_pop']
).transform_calculate(
year='parseInt(datum.year)',
Pill_per_pop='isValid(datum.Pill_per_pop) ? datum.Pill_per_pop : -1'
).encode(
color = alt.condition(
'datum.Pill_per_pop > 0',
alt.Color('Pill_per_pop:Q', scale=alt.Scale(scheme='blues')),
alt.value('#dbe9f6')
)).add_selection(
select_year
).properties(
width=700,
height=400
).transform_filter(
select_year
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…