2

I am trying to add pop_april_2010 values associated with each County to the map. However my code only returns the base map when I don't include the line "color='pop_april_2010:Q'". Including the line results in an empty image without the map.

Dataframe - df
The data is from Kaggle > https://www.kaggle.com/camnugent/california-housing-feature-engineering?select=cal_populations_county.csv

enter image description here

Code

url = 'https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/CA-06-california-counties.json'

source = alt.topo_feature(url, "cb_2015_california_county_20m")

alt.Chart(source).mark_geoshape().encode(
tooltip='properties.NAME:N',
# color='pop_april_2010:Q'
).transform_lookup(
    lookup='County',
    from_= alt.LookupData(cal2, 'County', ['pop_april_2010']),
).properties(
    width=500,
    height=300
).project(
    type='albersUsa'
).properties(
    width=500,
    height=300
)

Results

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159
IOIOIOIOIOIOI
  • 277
  • 3
  • 15

1 Answers1

0

The reason the chart is empty when using color is that the wrong column name is used in the lookup of the topojson file, so nothing is returned and you are passing a string referencing a non-existing column to color. If you inspect your topojson file you can see that each county's named is stored in the NAME attribute, not County.

Further, if you compare your topojson to what is in the vega sample data (for easier comparison paste them into a json viewer) you can see that the key used in the sample data file for the lookup example (id) is at the top level of each geometry object while in your fie it is nested one more level under properties. This means that you need to use 'properties.NAME' as the lookup string (more details about working geogrphical data in this answer):

import altair as alt
import pandas as pd

# From https://www.kaggle.com/camnugent/california-housing-feature-engineering?select=cal_populations_county.csv
cal2 = pd.read_csv('~/Downloads/cal_populations_county.csv')

url = 'https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/CA-06-california-counties.json'
source = alt.topo_feature(url, "cb_2015_california_county_20m")

alt.Chart(source).mark_geoshape().encode(
    tooltip='properties.NAME:N',
    color='pop_april_2010:Q'
).transform_lookup(
    lookup='properties.NAME',
    from_= alt.LookupData(cal2, 'County', ['pop_april_2010']),
).properties(
    width=500,
    height=300
).project(
    type='albersUsa'
)

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159