Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of rows of a CSV file, with d3.js

Suppose I have this CSV file

Day,What
2013-10-27,Apple
2013-10-27,Cake
2013-10-27,Apple
2013-10-28,Apple
2013-10-28,Apple
2013-10-28,Blueberry
2013-10-28,Orange

I would like to drawn with D3.js a time-series graph. All I need to do is to display the sum of the number of rows for each day. As an example, the 27 of November should have 3 as value, the 28th should have 4.

Is there a way I can archive it? I've been trying to create a new dataset out of the CSV one with no positive results.

var dataset;

d3.csv("data.csv", function(d) {
  return {
    Day: new Date(d.Day),
    What: d.What
  };
}, 

function(error, rows) {
    dataset = rows;
    // I SUPPOSE THE FUNCTION THAT GENERATE THE NEW DATASET SHOULD GO THERE     
});
like image 818
philapple Avatar asked Oct 31 '13 15:10

philapple


People also ask

How to count number of rows in CSV file using JavaScript?

var rowsn= csv. match(/(?:"(?:[^"]|"")*"|[^,\n]*)(?:,(?:"(?:[^"]|"")*"|[^,\n]*))*\n/g). length; If you are lucky enough to be dealing with a variant of CSV that complies with RFC4180's recommendation that there are no " characters in unquoted fields, you can make this a bit more readable.

How do I know how many rows CSV?

Using len() function Under this method, we need to read the CSV file using pandas library and then use the len() function with the imported CSV file, which will return an int value of a number of lines/rows present in the CSV file.

What does d3 CSV return?

d3. csv() returns the data as an object. This object is an array of objects loaded from your csv file where each object represents one row of your csv file.

Which function is used to count rows in a csv file Rownumber?

Determines the ordinal number of the current row within a group of rows, counting from 1, based on the ORDER BY expression in the OVER clause.


1 Answers

You can use the fast lookup of JavaScript Object properties as an index while you accumulate your counts:

var counts = {};
rows.forEach(function(r) {
    if (!counts[r.Day]) {
        counts[r.Day] = 0;
    }
    counts[r.Day]++;
});

Then you'll need to convert this back into an array so you can use it with D3:

var data = [];
Object.keys(counts).forEach(function(key) {
    data.push({
        day: key,
        count: counts[key]
    });
});

I'm assuming here that you want to ignore the "What" column because you only talked about aggregating a count of days and there are different "What" values for the same day. If you want to consider "What" while you count you can just build it into the key of your original counts object (r.Day + r.What).

EDIT: Based on comment, adding the code with a compound key.

var counts = {};
rows.forEach(function(r) {
    var key = r.Day + r.What;
    if (!counts[key]) {
        counts[key] = {
           day: r.Day,
           what: r.What,
           count: 0
        };
    }
    counts[key].count++;
});

var data = [];
Object.keys(counts).forEach(function(key) {
    data.push(counts[key]);
});
like image 140
Scott Cameron Avatar answered Nov 04 '22 07:11

Scott Cameron