Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created October 10, 2012 19:19
Show Gist options
  • Save robdodson/3867807 to your computer and use it in GitHub Desktop.
Save robdodson/3867807 to your computer and use it in GitHub Desktop.
Getting column names with d3.csv
<script type="text/javascript">
/*
Example cars.csv:
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
*/
d3.csv('cars.csv', function(csv) {
csv.forEach(function(row) {
console.log(Object.keys(row));
});
});
/*
For each row outputs: ["Year", "Make", "Model", "Length"]
*/
</script>
@hatemhosny
Copy link

d3.csv('cars.csv', function(csv) {
    console.log(csv.columns);  // ["Year", "Make", "Model", "Length"]
});

https://github.com/d3/d3-dsv#dsv_parse

@jtrimm007
Copy link

jtrimm007 commented Feb 18, 2021

@hatemhosny your method wasn't working. Here is the correct way to read in a csv file and get the column names:

d3.csv('csv/analytic_data2010.csv').then(function (data) { console.log(data.columns); });

Here is the link to the source: https://github.com/d3/d3-fetch/blob/master/README.md#installing. The only thing I added is the .columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment