Saturday, 16 November 2019

Is there a way to convert CSV columns into hierarchical relationships?

I have a csv of 7 million biodiversity records where taxonomy levels are as columns. For instance:

RecordID,kingdom,phylum,class,order,family,genus,species
1,Animalia,Chordata,Mammalia,Primates,Hominidae,Homo,Homo sapiens
2,Animalia,Chordata,Mammalia,Carnivora,Canidae,Canis,Canis
3,Plantae,nan,Magnoliopsida,Brassicales,Brassicaceae,Arabidopsis,Arabidopsis thaliana
4,Plantae,nan,Magnoliopsida,Fabales,Fabaceae,Phaseoulus,Phaseolus vulgaris

I want to create a visualization in D3, but data format must be a network, where each different value of column is a child of the previous column for a certain value. I need to go from the csv to something like this:

{
  name: 'Animalia',
  children: [{
    name: 'Chordata',
    children: [{
      name: 'Mammalia',
      children: [{
        name: 'Primates',
        children: 'Hominidae'
      }, {
        name: 'Carnivora',
        children: 'Canidae'
      }]
    }]
  }]
}

I haven't come up with an idea of how to do this without using a thousand for loops. Does anybody have a suggestion on how to create this network either on python or javascript?



from Is there a way to convert CSV columns into hierarchical relationships?

No comments:

Post a Comment