Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Populate the dataset with zero if rating for a particular year doesn't exist

My Original dataset has 4 columns name, average_rating, year and rating. I have to create linechart for count(a_rating), where 0<rating<9 vs rating. However if for a particular year rating doesn't exist, I need to replace he count with zero. e.g. if no rating 2 exists for year 2015, then year: 2015, rating: 2, count:0. I am not able to generate the zeros.

few rows of rating.csv->

name,year,average_rating,users_rated Codenames,2015,7.71148,51209 King of Tokyo,2011,7.23048,48611 Love Letter,2012,7.25253,47014 Splendor,2014,7.47331,45239 7 Wonders Duel,2015,8.12219,40971 Scythe,2016,8.2841,39962 Lords of Waterdeep,2012,7.76601,38696 Terraforming Mars,2016,8.40331,39327 The Castles of Burgundy,2011,8.12835,36901 Patchwork,2014,7.7122,33721 Dead of Winter: A Crossroads Game,2014,7.67747,32915 Terra Mystica,2012,8.19293,32719 Pandemic Legacy: Season 1,2015,8.64165,31326 Coup,2012,7.03729,29287

    d3.dsv(",", rating.csv, function (d) {
      return {
        // format data attributes if required
        name:d.name,
        year:d3.timeFormat("%Y")(new Date(d.year)),
        avgRating:Math.floor(+d["average_rating"]),
        users:+d['users_rated']
      }
    }).then(function (data) {

      const m = new Map();
      data.forEach(({year, avgRating}) => {
        // Create a key with values that we want to group by
        // A list of key-value pairs is chosen to make use of `Object.fromEntries` later
        if (year>2014){
        const hash = JSON.stringify([['year', year], ['rating', +avgRating]]);
        m.set(hash, (m.get(hash) || 0) + 1);}
      });

      const grouped = [...m].map(([rec, count]) => ({
        ...Object.fromEntries(JSON.parse(rec)),
        count,
      }))

  var sumstat = d3.nest() // nest function allows to group the calculation per level of a factor
    .key(function(d) { return d.year;})
    .entries(grouped);

console.log(sumstat)
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda