Function Repository Resource:

FromISOCountry

Source Notebook

Parse ISO 3166-1 country codes to a Country entity

Contributed by: Matthew W Thomas

ResourceFunction["FromISOCountry"][expr]

replaces ISO country codes in expr with "Country" entities.

ResourceFunction["FromISOCountry"][expr,levelspec]

replaces ISO country codes with "Country" entities in parts of expr specified by levelspec.

Details and Options

ResourceFunction["FromISOCountry"] converts strings that correspond to ISO country codes into the appropriate Entity.
ResourceFunction["FromISOCountry"] has the option "CountryFormat", which can accept either "ISO2" or "ISO3" to choose how to interpret country codes.

Examples

Basic Examples (3) 

Replace an ISO country code with an entity:

In[1]:=
ResourceFunction["FromISOCountry"]["AF"]
Out[1]=

Replace country codes in a List with entities:

In[2]:=
ResourceFunction["FromISOCountry"][{"FR", "ES", "IT"}]
Out[2]=

Specify the levelspec to replace only certain levels:

In[3]:=
ResourceFunction["FromISOCountry"][{"AF" -> {"US", "IS"}}, {2}]
Out[3]=

Options (1) 

CountryFormat (1) 

You can specify to use the three-character ISO3 format instead using "CountryFormat":

In[4]:=
ResourceFunction["FromISOCountry"][{"USA", "ISL"}, "CountryFormat" -> "ISO3"]
Out[4]=

Applications (2) 

Convert country codes in a Dataset to their respective Entity forms:

In[5]:=
dataset = Dataset[{
   <|"Country" -> "FR", "Tourists" -> 89.4`|>,
   <|"Country" -> "ES", "Tourists" -> 82.8`|>,
   <|"Country" -> "IT", "Tourists" -> 62.1`|>,
   <|"Country" -> "DE", "Tourists" -> 38.9`|>,
   <|"Country" -> "GB", "Tourists" -> 36.3`|>,
   <|"Country" -> "AT", "Tourists" -> 30.8`|>,
   <|"Country" -> "PT", "Tourists" -> 22.8`|>
   }]
Out[5]=
In[6]:=
Entitydataset = ResourceFunction["FromISOCountry"][dataset]
Out[6]=

Visualize the data with GeoRegionValuePlot:

In[7]:=
GeoRegionValuePlot[Apply[#1 -> #2 &, Entitydataset, {1}]]
Out[7]=

Publisher

Matthew W Thomas

Version History

  • 1.0.0 – 09 March 2020

Source Metadata

Related Resources

License Information