-
-
Save vlandham/74e0f1326cd48d10f2c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Reading in Data | |
// The first step in any data processing is getting the data! | |
// Here is how to parse in and prepare common input formats using D3.js | |
// ## Parsing CSV Files | |
// [D3 has a bunch](https://github.com/mbostock/d3/wiki/Requests) of filetypes it can support when loading data, and one of the most common is probably plain old CSV (comma separated values). | |
// Let's say you had a csv file with some city data in it: | |
// | |
// ``` | |
// cities.csv: | |
// | |
//city,state,population,land area | |
//seattle,WA,652405,83.9 | |
//new york,NY,8405837,302.6 | |
//boston,MA,645966,48.3 | |
//kansas city,MO,467007,315.0 | |
// ``` | |
// Use [d3.csv](https://github.com/mbostock/d3/wiki/CSV) to convert it into an array of objects | |
d3.csv("/data/cities.csv", function(data) { | |
console.log(data[0]); | |
}); | |
// ``` | |
//=> {city: "seattle", state: "WA", population: "900000", square miles: "12.3"} | |
// ``` | |
// | |
// You can see that the headers of the original CSV have been used as the property names for the data objects. | |
// Thus, using `d3.csv` in this manner requires that your CSV file has a header row. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment