Using multiple variables to generate a table or csv

In summary, you can use Mathematica's curated data sets to create a data set containing city information, country information, coordinates, and weather information. This can be done using a combination of CityData, CountryData, and WeatherData functions. To make the station name variable and loop through all cities, you can use a Table and Map command. Finally, you can export the data as a CSV file for further use.
  • #1
mlucas
1
0
Hi All,

I am new to mathematica and this kind of work in general, and was looking to score a little help on this thread. I basically need to build a data set that contains city information, country information, coordinates, and weather information. I want to do this using Mathematica's curated data sets. WeatherData, CityData, and CountryData. Below is what I have worked out so far:

Module[
{dateRange, mean, cdd, hdd, station, country, location, population,
GDPPerCapita,
reference = (65 - 32)/1.8, cumList},
station = "Chicago";
country = CityData[station, "Country"];
population = CityData[station, "Population"];
location = CityData[station, "Coordinates"];
GDPPerCapita = CountryData[country, "GDPPerCapita"];
dateRange = {{2011, 1, 1}, {2011, 12, 31}, "Day"};
mean = WeatherData[station, "MeanTemperature", dateRange];
cdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[# - reference, 0] & /@ mean[[All, 2]]}], 2];
hdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Min[# - reference, 0] & /@ mean[[All, 2]]}], 2];
cumList = Transpose[{Join[
Transpose[{cdd[[All, 1]]}],
Transpose[{Drop[FoldList[Plus, 0, cdd[[All, 2]] + hdd[[All, 2]]],
1]}],
2]}];
Grid[station, country, location, population, GDPPerCapita,
Last[cumList]]]


Which gives the following result...

Grid["Chicago", "UnitedStates", {41.8376, -87.6818}, 2695598, \
45230.2, {{{2011, 12, 31}, -2842.41}}]

Basically what I am looking for is a way to make the "station" variable, I want my function to cycle through all of the 160,000ish cities on the list and make a table record for each one.

The search term i want to use is simply the city name. If you enter:

CityData[]

the output looks like this {City, Region, Country} (for all of the cities in the world)

I'm sure this is possible, but i am a complete newbie when it comes to anything like this, I thought I would seek some expert advice.

So repeat myself, i just need the station name to be variable, and for the function to loop through all the cities, and to output to a table form or csv.

Thanks in advance,

Matt
 
Physics news on Phys.org
  • #2
One way to do this would be to use a Table and Map command. You can create a table of cities and their associated data like so:cityTable = Table[{CityData[i, "Name"], CityData[i, "Region"], CityData[i, "Country"], CityData[i, "Coordinates"], CountryData[CityData[i, "Country"], "GDPPerCapita"], WeatherData[CityData[i, "Name"], "MeanTemperature", dateRange]}, {i, CityData[]}];Then you can Map over each element in the cityTable to extract the information you need. For example:Map[{#[[1]], #[[3]], #[[4]], CountryData[#[[3]], "Population"], #[[5]], #[[6]]} &, cityTable]You can then export the data as a CSV file with Export["fileName.csv", data], where data is the result of the Map command.
 

FAQ: Using multiple variables to generate a table or csv

What is the purpose of using multiple variables to generate a table or csv?

Using multiple variables allows for the creation of a more comprehensive and organized data set. It also allows for the analysis of complex relationships between different variables.

How do you determine which variables to include in a table or csv?

The variables included in a table or csv should be relevant to the research question or topic being studied. They should also be measurable and have a clear relationship to each other.

What is the benefit of using a csv over a table?

A csv (comma-separated values) file allows for the easy transfer and sharing of data between different programs and platforms. It also allows for large amounts of data to be organized and analyzed more efficiently compared to a traditional table.

How should the variables be organized in a csv file?

The variables should be organized in columns, with each row representing a different data point. The first row should contain the variable names, and the subsequent rows should contain the corresponding data for each variable.

What is the best way to present the data from multiple variables in a table or csv?

The best way to present the data depends on the research question and the type of data being analyzed. Some options include using summary statistics, charts or graphs, or conducting further analysis such as regression or correlation.

Similar threads

Replies
1
Views
2K
Replies
0
Views
2K
Replies
4
Views
4K
3
Replies
96
Views
7K
Replies
4
Views
4K
Replies
16
Views
7K
Replies
13
Views
2K
Replies
2
Views
5K
Replies
21
Views
5K
Replies
10
Views
4K
Back
Top