Using Python to create a world map from a list of country names | by John Oh | Towards Data Science

Using Python to create a world map from a list of country names

From a list of country names, get latitude and longitude to create a world map

John Oh
Towards Data Science

--

Recently, I worked on a project to create a world map based on a list of short country names such as United States. Here, I laid out the steps involved to show how to create a world map (or any other maps).

Data

A sample data with two columns (Country Name, User Percent) is our raw data.

1. Conversion to Alpha 2 codes and Continents

The alpha 2 codes are easier to work with for later analysis, so the short country names are converted to alpha 2 country codes. For example, United States is converted to US. Python’s pycountry-convert package is used to handle the conversion. The below Python code snippet shows a function to convert.

#installation
pip install pycountry-convert
#function to convert to alpah2 country codes and continentsfrom pycountry_convert import country_alpha2_to_continent_code, country_name_to_country_alpha2def get_continent(col):
try:
cn_a2_code = country_name_to_country_alpha2(col)
except:
cn_a2_code = 'Unknown'
try:
cn_continent = country_alpha2_to_continent_code(cn_a2_code)
except:
cn_continent = 'Unknown'
return (cn_a2_code, cn_continent)

After this step, the raw data is processed as follows:

2. Get longitude and latitude

Second, longitude and latitude information are extracted based on these alpha 2 country codes. Python’s geopy makes it easy to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources. The below Python code snippet shows a function to get longitude and latitude.

#installation
pip install geopy
#function to get longitude and latitude data from country namefrom geopy.geocoders import Nominatimgeolocator = Nominatim()
def geolocate(country):
try:
# Geolocate the center of the country
loc = geolocator.geocode(country)
# And return latitude and longitude
return (loc.latitude, loc.longitude)
except:
# Return missing value
return np.nan

The table below is shown after running the function above and splitting geolocate into two separate latitude and longitude columns.

3. Create a world map

There are many Python packages to create visually attractive and informative maps — basemap, bokeh, and folium among others. Here, folium is used to create a world map of certain user distributions. Folium’s CircleMarker() is useful to describe the data by varying radius and color variables.

#installation
pip install folium
# Create a world map to show distributions of users
import folium
from folium.plugins import MarkerCluster
#empty map
world_map= folium.Map(tiles="cartodbpositron")
marker_cluster = MarkerCluster().add_to(world_map)#for each coordinate, create circlemarker of user percent
for i in range(len(df)):
lat = df.iloc[i]['Latitude']
long = df.iloc[i]['Longitude']
radius=5
popup_text = """Country : {}<br>
%of Users : {}<br>"""
popup_text = popup_text.format(df.iloc[i]['Country'],
df.iloc[i]['User_Percent']
)
folium.CircleMarker(location = [lat, long], radius=radius, popup= popup_text, fill =True).add_to(marker_cluster)
#show the map
world_map

User can zoom in the map to see more detailed user distributions by country.

In this example, I created a world map with the help of several Python packages. Needless to say, other types of geomaps can be easily created with given coordinates.

--

--

Recommended from Medium

Lists

See more recommendations