Project 4: Exploring the UK's hop-based product imports and exports
produced by Jez Phipps on 7th November 2016.
This is my project notebook for Week 4 of The Open University's Learn to Code for Data Analysis course.
A country's economy depends, sometimes heavily, on its exports and imports. The United Nations Comtrade database provides data on global trade. It will be used to analyse the UK's imports and exports of three hop-based products (see commodity descriptors below) in 2014:
How much does the UK export and import and is the balance positive (more exports than imports)?
Which are the main trading partners, i.e. from/to which countries does the UK import/export the most?
Which are the regular customers, i.e. which countries buy the products from the UK every month?
Which countries does the UK both import from and export to?
Option to toggle code visibility on/off is at bottom of page.
Getting and preparing the data
The data is obtained from the United Nations Comtrade website, by selecting the following configuration:
Type of Product: goods
Frequency: monthly
Periods: All of 2014
Reporter: United Kingdom
Partners: all
Flows: imports and exports
HS (as reported) commodity codes: 1210 (Hop cones, fresh or dried, lupulin), 121010 (Hop cones, not ground, powdered or pelleted), 130213 (Hop extract)
Clicking on 'Preview' results in a message that the data exceeds 500 rows. Data was downloaded using the Download CSV button and the download file renamed appropriately.
The data can also be downloaded directly from Comtrade using the "View API Call" URL, modified in two ways:
max=500
is increased tomax=5000
to make sure all data is loaded,&fmt=csv
is added at the end to obtain the data in CSV format.
On reading in the data, the commodity code has to be read as a string, to not lose the leading zero.
Classification | Year | Period | Period Desc. | Aggregate Level | Is Leaf Code | Trade Flow Code | Trade Flow | Reporter Code | Reporter | ... | Qty | Alt Qty Unit Code | Alt Qty Unit | Alt Qty | Netweight (kg) | Gross weight (kg) | Trade Value (US$) | CIF Trade Value (US$) | FOB Trade Value (US$) | Flag | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1045 | HS | 2014 | 201412 | December 2014 | 6 | 1 | 1 | Imports | 826 | United Kingdom | ... | NaN | NaN | NaN | NaN | 72679 | NaN | 1903711 | NaN | NaN | 0 |
1046 | HS | 2014 | 201412 | December 2014 | 6 | 1 | 2 | Exports | 826 | United Kingdom | ... | NaN | NaN | NaN | NaN | 1670 | NaN | 104985 | NaN | NaN | 0 |
2 rows × 35 columns
Most columns are irrelevant for this analysis, or contain always the same value, like the year and reporter columns. The commodity code is transformed into a short but descriptive text and only the relevant columns are selected.
Period | Partner | Trade Flow | Hop-based products | Trade Value (US$) | |
---|---|---|---|---|---|
0 | 201401 | World | Imports | cones_1 | 2875045 |
1 | 201401 | World | Exports | cones_1 | 537108 |
2 | 201401 | Australia | Imports | cones_1 | 10763 |
3 | 201401 | Australia | Exports | cones_1 | 66033 |
4 | 201401 | Belgium | Imports | cones_1 | 493981 |
The data contains the total imports and exports per month, under the 'World' partner. Those rows are removed to keep only the per-country data.
Period | Partner | Trade Flow | Hop-based products | Trade Value (US$) | |
---|---|---|---|---|---|
2 | 201401 | Australia | Imports | cones_1 | 10763 |
3 | 201401 | Australia | Exports | cones_1 | 66033 |
4 | 201401 | Belgium | Imports | cones_1 | 493981 |
5 | 201401 | Belgium | Exports | cones_1 | 19782 |
6 | 201401 | Canada | Exports | cones_1 | 9290 |
Total trade flow
To answer the first question, 'How much does the UK export and import and is the balance positive (more exports than imports)?', the dataframe is split into two groups: exports from the UK and imports into the UK. The trade values within each group are summed up to get the total trading.
Main trade partners
To address the second question, 'Which are the main trading partners, i.e. from/to which countries does the UK import/export the most?', the dataframe is split by country instead, and then each group aggregated for the total trade value. This is done separately for imports and exports. The result is sorted in descending order so that the main partners are at the top.
The export values can be plotted as a bar chart, making differences between countries easier to see.
Regular importers
Given that there are three commodities, the third question, 'Which are the regular customers, i.e. which countries buy hop-based exports from the UK every month?', is meant in the sense that a regular customer buys (imports) all three commodities every month. This means that if the exports dataframe is grouped by country, each group has exactly 36 rows (three commodities bought each of the 12 months). To see the countries, only the first month of one commodity has to be listed, as by definition it's the same countries every month and for the other commodity.
Period | Partner | Trade Flow | Hop-based products | Trade Value (US$) | |
---|---|---|---|---|---|
662 | 201401 | France | Exports | extract | 63928 |
667 | 201401 | Ireland | Exports | extract | 336060 |
Bi-directional trade
To address the fourth question, 'Which countries does the UK both import from and export to?', a pivot table is used to list the total export and import value for each country.
Trade Flow | Exports | Imports |
---|---|---|
Partner | ||
Argentina | 359284 | NaN |
Australia | 1370282 | 1673828 |
Austria | 1335 | NaN |
Belarus | 110361 | NaN |
Belgium | 5664460 | 8874391 |
Removing the rows with a missing value will result in only those countries with bi-directional trade flow with the UK.
Trade Flow | Exports | Imports |
---|---|---|
Partner | ||
Australia | 1370282 | 1673828 |
Belgium | 5664460 | 8874391 |
Canada | 601190 | 3024 |
China | 430125 | 56097 |
Czech Rep. | 254642 | 1998597 |
Denmark | 593679 | 122416 |
France | 1221984 | 9413829 |
Germany | 9158415 | 17281076 |
India | 121934 | 38215 |
Netherlands | 752854 | 68009 |
New Zealand | 265622 | 2176340 |
Peru | 1941 | 4693 |
Poland | 375610 | 2758898 |
Slovakia | 38916 | 582680 |
Slovenia | 40334 | 6092866 |
Spain | 659212 | 1989 |
United States of America | 9431078 | 39915608 |
Conclusions
The UK hops-based product trade in 2014 was analysed in terms of which countries the UK mostly depends on for income (exports) and goods (imports). Over the period, the UK had a trade surplus of over 32 million US dollars with 74 different parther countries.
The U.S. is the main partner, but the UK imported almost over three times the value of hop-based goods from the U.S. than it exported to them (i.e. a substantial trade deficit of about 31 million US dollars).
The UK exported to 72 countries during the period, but only imported from 19 countries. Germany and France are the next two main customers for both hop imports into, and exports out of, the UK.
Interestingly, the UK is not very heavily dependent on its regular customers, since there are only two countries out of 74 that buy all three types of hops-based commodities every month (France and Ireland). This accounts for less than 1% of exports. However, the UK does rely significantly on France for imported goods.
The UK has bi-directional trade (i.e. both exports and imports) with 17 countries, although for some (i.e. Peru) the trade value is suprisingly low.