How to filter out some extraneous columns from .csv for grouped bar chart?

3,008 views
Skip to first unread message

Maggie Lee

unread,
Apr 30, 2014, 4:32:21 PM4/30/14
to d3...@googlegroups.com
I want to make a grouped bar chart, but my .csv has 20 columns. Only 4 have the data I need.  The other 16 columns I might need later, so I don't want to just open my .csv in Excel and kill the other 16 columns. 

I'm following the canonical example, and I think the answer is in these key lines, but I don't understand them.


d3.csv("data.csv", function(error, data) {
  var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "State"; });

  data.forEach(function(d) {
    d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
  });

How would one modify the code if data.csv had extraneous columns as so?
I suspect the logic will be something like "select the columns where column name includes "Years" or "State."
State,Under 5 Years,5 to 13 Years,14 to 17 Years,18 to 24 Years,25 to 44 Years,45 to 64 Years,6 Years and Over, Random_Four_Digit_Code, Commonest_Bird
CA,2704659,4499890,2159981,3853788,10604510,8819342,4114496, 0264, Crow
TX,2027307,3277946,1420518,2454721,7017731,5656528,2472223, 0693, Robin
NY,1208495,2141490,1058031,1999120,5355235,5120254,2607672, 1808, Bluebird

Thanks!
-Maggie

Japhy Bartlett

unread,
May 1, 2014, 12:57:05 PM5/1/14
to d3...@googlegroups.com
Probably want something like:

.filter(function(key) { return key == "Random_Four_Digit_Code" || key == "Commonest_Bird"; })
with the caveat that I'm not sure about the details of filter() offhand.  you could also make a copy of the csv that's stripped down to what you need - if you're serving this on a website, it will make the file smaller and the page load faster.

another route, in the .map() function, only return the columns you want!


--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maggie Lee

unread,
May 1, 2014, 3:27:13 PM5/1/14
to d3...@googlegroups.com
hey thanks that worked great!
Reply all
Reply to author
Forward
0 new messages