kthieu
kthieu

Reputation: 197

d3 reading csv/tsv files if column names are numbers?

Doing a project using data from the world bank. Their data is organized such that they have individual years as column names in their csv. Aside from possibly changing the name, how would I go about accessing them using d3.csv and mapping them into a custom array since I won't use all the values.

For example, the file I'm using is GDP per country. Each element/row is formated like so

"Country Name","Country Code","Indicator Name","Indicator Code", "1960","1961","1962","1963","1964","1965","1966","1967","1968","1969","1970","1971","1972","1973","1974","1975","1976","1977","1978","1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013",

If I wanted the GDP values for a Country like United Kingdom, Brazil, China, Russia, US, etc, and for years 2004-2012, how would I go about it?

Would the code looks something like this

d3.csv(URL, function(d) {
     return {
         AttributeName : d.ColumnName
         //Continues for all columns I need
     };

});

Again, d.ColumnName won't work if the column name is an actual integer. How would I account for spaces in the column name as shown as well? How would I also go about displaying the elements properly in say the document itself or the console?

I apologize for so many questions. Feel free to direct me to solutions. Thanks.

Thanks to Lars for the answer. I'm gonna append the next step of what I want to do. Now if I wanted to make a Line Graph using this data, how would I access the resulting array of objects? Again, I only want specific countries out of the 200+ element Array.

Upvotes: 2

Views: 4143

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109242

You can solve all of these issues by using the alternative syntax for accessing attributes -- instead of

d.foo

you can write

d["foo"]

This also works with spaces, numbers, etc:

d["11111"]
d["string with spaces"]

Upvotes: 5

Related Questions