Ramis Ramis - 6 months ago 18
Javascript Question

How to export particular columns in CSV format?

I have data.csv file:

Q,W,E,R,T,Y
A,S,D,F,G,H


To load this file I am using this code:

d3.text("data.csv", function(data) {
parsedCSV = d3.csv.parseRows(data);
// Some data manipulations...
// How to export 2nd & 4th column in CSV format?
}


How to export 2nd and 4th column from parsedCSV object in CSV fromat?

Answer

Try:

d3.text("data.csv", function(data) {
    var header = data.split('\n')[0].split(',').map(function(name) {
        return name.trim();
    });
    var parsedCSV = d3.csv.parseRows(data);
    var columns = parsedCSV.map(function(row) {
        return [row[header[1]], row[header[3]]];
    });
});