d3.group & join on NYTimes Covid-19 Data

Hello!

I’ve run into relatively basic issue trying alter the raw data for the NYTimes Covid tracking on the county level. It is an array of objects that look like this:

https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv

[{"date":"2020-03-13","county":"Elmore","state":"Alabama","fips":"01051","cases":"1","deaths":"0","population":0}]

Here is my notebook:

I am trying to manipulate the array with my own county level data for Alabama, my home state. I have a .csv with population, demographics, and more to include in my graphic.

Unfortunately, I cannot find the right way to do the data join so that all the array entries from a county also populate with the new data.

I have never used d3 selection before and am having difficulty applying it to this massive data set.

Any help would be appreciated. I can explain the problem in more detail, but this is not a specific bug. I have tried many approaches, none of which quite get the job done.

A Map is often useful for this. d3.group will create nested maps for you, but you can also construct them directly like so:

popByCounty = new Map(al_county_pop.map(d => [d.county, d.population]))

Then later when you want to get the population for a specific county in Alabama, you can say:

popByCounty.get("Jefferson")

If you want to join this together with the NYT data, you could also say:

raw_county_data
  .filter(a => a.state === "Alabama")
  .map(d => ({...d, population: popByCounty.get(d.county)}))
1 Like

Incredible…

Thank you!