Accessing CSV data column wise

736 views
Skip to first unread message

sathish kr

unread,
Jul 16, 2014, 4:06:51 AM7/16/14
to d3...@googlegroups.com
I have a CSV file which I want to read data column wise. 

A, 1
B, 2
C, 3

I want to create HTML table like 
A , B , C
1, 2, 3

So Please let me know how I can read csv columnwise

Skip Montanaro

unread,
Jul 16, 2014, 6:16:55 AM7/16/14
to d3...@googlegroups.com
> I have a CSV file which I want to read data column wise.
>
> A, 1
> B, 2
> C, 3
>
> I want to create HTML table like
> A , B , C
> 1, 2, 3


I've no experience with doing this in JavaScript, but I doubt the
problem is d3-specific. You will have to read the data in the usual
fashion then transpose it. Here's a StackOverflow topic about
transposing a 2d array:

http://stackoverflow.com/questions/4492678/to-swap-rows-with-columns-of-matrix-in-javascript-or-jquery

The handler callback passed to d3 could just call

t = data.transpose()

Skip

Curran

unread,
Jul 16, 2014, 1:12:12 PM7/16/14
to d3...@googlegroups.com
Hello,


Also, here's some JavaScript code for transposing a table as you need:

// "data" is what you would get from using D3.csv() to parse your file (first you need to add the column names as the first entry, otherwise it is not valid CSV).
var data = [
      {
        letter:"A",
        number: 1
      },
      {
        letter:"B",
        number: 2
      },
      {
        letter:"C",
        number: 3
      }
    ],
    transposed = {};
data.forEach(function(entry){
  transposed[entry.letter] = entry.number;
});
console.log(transposed);// prints {A: 1, B: 2, C: 3} 

Best,
Curran
Reply all
Reply to author
Forward
0 new messages