# Find the average GDP of multiple countries at once

How would I find the average GDP of a list of countries between set years?

I know how to do it for one but I'm not sure about multiple values. I also wish to find the average population size.

An example of what I want: a list of the average GDPs of each individual country France, Germany, Spain, Russia, Portugal and Poland between the years 1999 and 2008.

How would I do the same for population?

Here is another approach using EntityValue. The countries:

countries = Map[
Entity["Country",#]&,
{"France","Germany","Spain","Russia","Portugal","Poland"}
];


Using EntityValue:

interval = Interval[{DateObject[{1999}], DateObject[{2008}]}];

dataset = EntityValue[
countries,
{
EntityProperty["Country","GDP",{"Date"->interval}],
EntityProperty["Country","Population",{"Date"->interval}]
},
"Dataset"
];


The dataset contains the information you want, although the values are encoded in TimeSeries/TemporalData objects. For instance:

ts = dataset[[1,1]];


TemporalData

To extract values from a TimeSeries/TemporalData object you give it the date you are interested in:

ts[DateObject[{2004}]]


Quantity[2.12411*10^12, ("USDollars")/("Years"), {}]

So, to obtain the population of Russia in 2004:

dataset[Entity["Country", "Russia"], 2, #[DateObject[{2004}]]&]


Quantity[144306982, "People", {}]

Or, the average GDP for all of the countries for the year 2002:

dataset[Mean, 1, #[DateObject[{2002}]]&]


Quantity[8.27107*10^11, ("USDollars")/("Years")]

Plot of the average GDP from 1999 to 2008:

dataset[DateListPlot @* Mean, 1] Plot of the GDP for each country from 1999 to 2008:

dataset[DateListPlot,1] Distribution of population in the year 2003:

PieChart[
dataset[All, 2, #[DateObject[{2003}]]&],
ChartLabels->Callout[Automatic]
] Here are two ways:

CountryData["Sweden", {"GDP", {1999, 2008}}] // Mean


and

ts = Entity["Country", "Sweden"][
EntityProperty["Country", "GDP", {
"CurrencyUnit" -> "CurrentUSDollar",
"Date" -> All
}]
];
TimeSeriesWindow[ts, {{1999, 1, 1}, {2008, 1, 1}}] // Mean


We can use free-form input to get the entities. Search for "historic GDP Sweden", to get the entities used in the snippet here above.

To do this for the population, we would proceed in the same way. To do it for other countries, we would simply need to replace "Sweden" in the above code. Example:

Mean@CountryData[#, {"GDP", {1999, 2008}}] & /@ {"France", "Germany", "Spain"} • @hellohi All data is not exposed through CountryData. For these cases, switch to using the entity approach. Start by finding the international migrant stock entity property by using free form input. – C. E. Mar 24 '18 at 19:09

Here is another way.

countries = {"France", "Germany", "Spain", "Russia", "Portugal", "Poland"};
yearMin = 1999; yearMax = 2008;

With[{property = "GDP"},
ds = AssociationThread[Range[yearMin, yearMax] -> (Association /@
Table[country -> CountryData[country, {property, year}],
{year, yearMin, yearMax}, {country, countries}])] // Dataset] and then

ds[Mean, All] // Normal // QuantityMagnitude
<|"France" -> 1.983877562406246*^12, "Germany" -> 2.656085060866531*^12,
"Spain" -> 1.0072285825063357*^12, "Russia" -> 6.843191640802267*^11,
"Portugal" -> 1.763798635199338*^11, "Poland" -> 2.806974107570857*^11|>


If you want just the means themselves:

Mean /@ Association[Table[country -> CountryData[country, {"GDP", {yearMin, yearMax}}],
{country, countries}]] // QuantityMagnitude