score:0

You could use d3.rollup or d3.rollups (d3 v6). Something like:

d3.csv("csv_file.csv").then(function(data) {
    var count_by_source = d3.rollup(data, v => v.length, d => d.source);
});

EDIT: After clarification, it became clear that react-d3-library is being used, which is no longer maintained since several years ago. Below are the answers for older versions of d3, but I would recommend that you load d3 directly in your html, or npm install d3, rather than this React wrapper.

For d3 v5, there is d3.nest which is a little different.

d3.csv("csv_file.csv").then(function(data) {
    var count_by_source = d3.nest().key(d => d.source)
        .rollup(v => v.length)
        .entries(data);
});

For d3 v4 or older, the d3.csv function was different from the current version.

d3.csv("csv_file.csv", function(data) {
    var count_by_source = d3.nest().key(d => d.source)
        .rollup(v => v.length)
        .entries(data);
});

Here is an example:

data = [{
  "row_hash": "MDBPD0000-C419771020-01-01",
  "source": "BALTIMORE-MAIN",
  "agency_name": "BALTIMORE-PD",
  "agency_ori": "MDBPD0000",
  "agency_incident_id": "17V0351",
  "occurred_date": "2017-07-26"
}, {
  "row_hash": "MDBPD0000-B63BECA76C-01-01",
  "source": "BALTIMORE-MAIN",
  "agency_name": "BALTIMORE-PD",
  "agency_ori": "MDBPD0000",
  "agency_incident_id": "17H0198",
  "occurred_date": "2017-07-26"
}, {
  "row_hash": "MDBPD0000-B63BECA76C-01-01",
  "source": "ALABAMA_MAIN",
  "agency_name": "BALTIMORE-PD",
  "agency_ori": "MDBPD0000",
  "agency_incident_id": "17H0198",
  "occurred_date": "2017-07-26"
}, {
  "row_hash": "MDBPD0000-B63BECA76C-01-01",
  "source": "ALABAMA_MAIN",
  "agency_name": "BALTIMORE-PD",
  "agency_ori": "MDBPD0000",
  "agency_incident_id": "17H0198",
  "occurred_date": "2017-07-26"
}, {
  "row_hash": "MDBPD0000-B63BECA76C-01-01",
  "source": "ALABAMA_MAIN",
  "agency_name": "BALTIMORE-PD",
  "agency_ori": "MDBPD0000",
  "agency_incident_id": "17H0198",
  "occurred_date": "2017-07-26"
}, {
  "row_hash": "MDBPD0000-B63BECA76C-01-01",
  "source": "ALABAMA_MAIN",
  "agency_name": "BALTIMORE-PD",
  "agency_ori": "MDBPD0000",
  "agency_incident_id": "17H0198",
  "occurred_date": "2017-07-26"
}]

var count_by_source = d3.nest().key(d => d.source)
  .rollup(v => v.length)
  .entries(data);
console.log(count_by_source)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.1/d3.min.js"></script>


Related Query

More Query from same tag