Recently, there’s been a lot of talk about a reduction in consumer spending in economies around the world so it got me thinking about who are Ireland’s biggest trading partners. Fortunately, the wonderful people over at the Central Statistics Office publish this data and more but for now, I’m going to focus on creating a map in Python of total Irish exports since 2015 to identify who we’ve been exporting to the most.
Most programming tools have mapping functionalities with some easier to use than others. I prefer using folium in Python because unlike packages like ggmap in R, we don’t need to sign up for API’s and it’s relatively straight forward to use.
Before we’re able to map the data, we need to preprocess it so that it’s in the correct format for mapping – and to be honest, data preprocessing is generally the most time consuming part of data science! The main elements of preprocessing for this dataset was to:
- create a full record per row
- replace blank values with 0’s
- remove any total or blank columns
- convert all remaining variables to numeric form
- aggregate exports per country
- create a new column calculating each country’s percentage value of total exports
- assign countries their longitudes & latitudes for mapping
Once preprocessing is complete we can move onto the fun stuff! Below is an excerpt of the code used to create our map of exports:
# Initialize the base map - these are starting points for the map: m3 = folium.Map(location=[20, 0], zoom_start=2) # defining the settings for the chloropleth: m3.choropleth( geo_data=country_shapes, # using the country shapes we got the json file name='Total Irish Exports between 2015 & 2020', # name of our map data=countries_total, # what data we want to map columns=['country_codes', 'perc'], # what columns from our dataframe that we want to map key_on='feature.id', # what we're matching with - in our instance, we're joining the country codes to the IDs in the json file fill_color='YlGnBu', # the colours we want to use in the ma p fill_opacity=0.5, # similar to transparency - colour setting line_opacity=0.5, legend_name='Percentage Value of Exports', # the name under our legend smooth_factor=0, # lines around the countries as we zoom in highlight=True, # does the map highlight the country when we hover over it with a mouse ) folium.LayerControl().add_to(m3)
The choropleth function allows us to colour the map depending on the value attributed with each country. In this instance, the percentage of total exports column was used. The higher the percentage, the darker the colour. Surprisingly, the US are the biggest importers of Irish goods (in total € value) with more than double the total value of the next largest importer, Belgium, who are closely followed by Great Britain. The countries in black are unrecorded in our dataset (the joys of working with data – some of it simply isn’t available or falls into a category like ‘other countries’).
The downside to using something like choropleth is that when values are very similar it’s difficult to differentiate between variables. In the above map, countries between 0 and 5% of total exports are indistinguishable from each other so in this instance a simple bar chart may be more suitable depending on your needs.
an alternative map in Tableau
At my day job, I’ve been working with Tableau and I’m always pleasantly surprised at its functionality so long as your data has been preprocessed using a programming language like Python or SQL. So, here’s what it looks like in Tableau.
Which is nicer? I’ll let you decide!
Thanks for stopping by. If you’re interested in trying it for yourself, the code and data have been included below.