Jupyter notebook Learn to code OU/Week4/My_Week_4_project.ipynb
Project 4: Exploring the UK's milk imports and exports
by Tony Hirst and Michel Wermelinger, 19 November 2015 and 28 March 2016 with ammendments by Chris Pyves 17th November 2016.
This is the 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 milk and cream in 2015:
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 milk from the UK every month?
Which countries does the UK both import from and export to?
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: January to May of 2015 [note datafile says Jan_Jul_2015]
Reporter: United Kingdom
Partners: all
Flows: imports and exports
HS (as reported) commodity codes: 0401 (Milk and cream, neither concentrated nor sweetened) and 0402 (Milk and cream, concentrated or sweetened)
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 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
604 | HS | 2014 | 201405 | May 2014 | 4 | 0 | 2 | Exports | 826 | United Kingdom | ... | NaN | NaN | NaN | NaN | 18480 | NaN | 30162 | NaN | NaN | 0 |
605 | HS | 2014 | 201405 | May 2014 | 4 | 0 | 2 | Exports | 826 | United Kingdom | ... | NaN | NaN | NaN | NaN | 50000 | NaN | 313417 | NaN | NaN | 0 |
2 rows × 35 columns
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 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
635 | HS | 2015 | 201505 | May 2015 | 4 | 0 | 2 | Exports | 826 | United Kingdom | ... | NaN | NaN | NaN | NaN | 2213 | NaN | 37883 | NaN | NaN | 0 |
636 | HS | 2015 | 201505 | May 2015 | 4 | 0 | 2 | Exports | 826 | United Kingdom | ... | NaN | NaN | NaN | NaN | 1588 | NaN | 5676 | NaN | NaN | 0 |
2 rows × 35 columns
The data in the two files above only covers the first five months of 2014 & 2015. 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.
Year | Period | Partner | Trade Flow | Milk and cream | Trade Value (US$) | |
---|---|---|---|---|---|---|
601 | 2014 | 201405 | United Arab Emirates | Exports | processed | 243150 |
602 | 2014 | 201405 | Uganda | Exports | processed | 415292 |
603 | 2014 | 201405 | Egypt | Exports | processed | 941909 |
604 | 2014 | 201405 | United Rep. of Tanzania | Exports | processed | 30162 |
605 | 2014 | 201405 | Burkina Faso | Exports | processed | 313417 |
Year | Period | Partner | Trade Flow | Milk and cream | Trade Value (US$) | |
---|---|---|---|---|---|---|
632 | 2015 | 201505 | Sweden | Exports | processed | 569 |
633 | 2015 | 201505 | Switzerland | Exports | processed | 16713 |
634 | 2015 | 201505 | United Arab Emirates | Exports | processed | 338823 |
635 | 2015 | 201505 | Turkey | Exports | processed | 37883 |
636 | 2015 | 201505 | United Rep. of Tanzania | Exports | processed | 5676 |
Year | Period | Partner | Trade Flow | Milk and cream | Trade Value (US$) | |
---|---|---|---|---|---|---|
1238 | 2015 | 201505 | Sweden | Exports | processed | 569 |
1239 | 2015 | 201505 | Switzerland | Exports | processed | 16713 |
1240 | 2015 | 201505 | United Arab Emirates | Exports | processed | 338823 |
1241 | 2015 | 201505 | Turkey | Exports | processed | 37883 |
1242 | 2015 | 201505 | United Rep. of Tanzania | Exports | processed | 5676 |
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.
Year | Period | Partner | Trade Flow | Milk and cream | Trade Value (US$) | |
---|---|---|---|---|---|---|
1238 | 2015 | 201505 | Sweden | Exports | processed | 569 |
1239 | 2015 | 201505 | Switzerland | Exports | processed | 16713 |
1240 | 2015 | 201505 | United Arab Emirates | Exports | processed | 338823 |
1241 | 2015 | 201505 | Turkey | Exports | processed | 37883 |
1242 | 2015 | 201505 | United Rep. of Tanzania | Exports | processed | 5676 |
Period | 201401 | 201402 | 201403 | 201404 | 201405 | 201501 | 201502 | 201503 | 201504 | 201505 |
---|---|---|---|---|---|---|---|---|---|---|
Year | ||||||||||
2014 | 139512730 | 111789783 | 131100878 | 139406385 | 144276259 | NaN | NaN | NaN | NaN | NaN |
2015 | NaN | NaN | NaN | NaN | NaN | 86330673 | 76215898 | 84114502 | 85389026 | 89463540 |
Year | 2014 | 2015 |
---|---|---|
Month | ||
1 | 139512730 | 86330673 |
2 | 111789783 | 76215898 |
3 | 131100878 | 84114502 |
4 | 139406385 | 85389026 |
5 | 144276259 | 89463540 |
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.
Year | 2014 | 2015 | |||
---|---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports | |
Month | Milk and cream | ||||
1 | processed | 40215103 | 30423330 | 26195709 | 19770570 |
unprocessed | 46923551 | 21950746 | 26259790 | 14104604 | |
2 | processed | 32298379 | 20614513 | 21025054 | 17405593 |
unprocessed | 40191337 | 18685554 | 25359029 | 12426222 | |
3 | processed | 42987355 | 26335257 | 26293698 | 19060366 |
unprocessed | 43794069 | 17984197 | 26028318 | 12732120 | |
4 | processed | 52900517 | 24770338 | 29029756 | 15331783 |
unprocessed | 42295261 | 19440269 | 25981444 | 15046043 | |
5 | processed | 55987927 | 26409462 | 29415739 | 16511555 |
unprocessed | 40213208 | 21665662 | 29441124 | 14095122 |
Year | 2014 | 2015 | |||
---|---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports | |
Month | Milk and cream | ||||
1 | processed | 40215103 | -30423330 | 26195709 | -19770570 |
unprocessed | 46923551 | -21950746 | 26259790 | -14104604 | |
2 | processed | 32298379 | -20614513 | 21025054 | -17405593 |
unprocessed | 40191337 | -18685554 | 25359029 | -12426222 | |
3 | processed | 42987355 | -26335257 | 26293698 | -19060366 |
unprocessed | 43794069 | -17984197 | 26028318 | -12732120 | |
4 | processed | 52900517 | -24770338 | 29029756 | -15331783 |
unprocessed | 42295261 | -19440269 | 25981444 | -15046043 | |
5 | processed | 55987927 | -26409462 | 29415739 | -16511555 |
unprocessed | 40213208 | -21665662 | 29441124 | -14095122 |
These figures indicate that in the first five months Jan - May the UK trade surplus has declined by 100m dollars from 209m dollars in 2014 to 109m dollars in 2015.
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.
Year | 2014 | 2015 | ||
---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports |
Partner | ||||
Ireland | 200713266 | 75342679 | 128155891 | 46263897 |
France | 19856738 | 43094286 | 9381762 | 28314091 |
Germany | 17966838 | 29234902 | 10828464 | 21899123 |
Netherlands | 38625707 | 25742755 | 18018603 | 17658912 |
Belgium | 20506014 | 26762776 | 7209121 | 14325697 |
The UK import values by country can be plotted as a bar chart, making differences between countries & year easier to see.
From these results it can be seen that in 2015 the UK reduced imports from its top 5 suppliers (in the first five months compared to 2014).
len | sum | |||
---|---|---|---|---|
Year | 2014 | 2015 | 2014 | 2015 |
Trade Flow | Imports | Imports | Imports | Imports |
Partner | ||||
Ireland | 10 | 10 | 75342679 | 46263897 |
France | 10 | 10 | 43094286 | 28314091 |
Germany | 10 | 10 | 29234902 | 21899123 |
Netherlands | 10 | 10 | 25742755 | 17658912 |
Belgium | 10 | 10 | 26762776 | 14325697 |
Denmark | NaN | 10 | NaN | 13681759 |
Poland | 10 | 10 | 4336432 | 4933917 |
Sweden | 10 | 10 | 3107260 | 1962413 |
Italy | 10 | 10 | 542209 | 321635 |
Lithuania | 10 | 10 | 186038 | 318407 |
Portugal | NaN | 10 | NaN | 308654 |
Now lets look at who the UK exports to:
Year | 2014 | 2015 |
---|---|---|
Trade Flow | Exports | Exports |
Partner | ||
Ireland | 200713266 | 128155891 |
Netherlands | 38625707 | 18018603 |
China | 7700931 | 14111163 |
Germany | 17966838 | 10828464 |
China, Hong Kong SAR | 8637173 | 9482458 |
Year | 2014 | 2015 |
---|---|---|
Trade Flow | Exports | Exports |
Partner | ||
Croatia | 17 | 5 |
Romania | 5829 | 36 |
Slovakia | 314 | 65 |
Estonia | 881 | 102 |
Bulgaria | 198 | 216 |
Luxembourg | 17006 | 448 |
Latvia | 41 | 567 |
Lithuania | 1216 | 1172 |
Ethiopia | NaN | 2188 |
Czech Rep. | 78055 | 2590 |
Regular importers
Given that there are two commodities, the third question, 'Which are the regular customers, i.e. which countries buy milk from the UK every month?', is meant in the sense that a regular customer imports both commodities every month. This means that if the exports dataframe is grouped by country, each group has exactly ten rows (two commodities bought each of the five 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.
len | sum | |||
---|---|---|---|---|
Year | 2014 | 2015 | 2014 | 2015 |
Trade Flow | Exports | Exports | Exports | Exports |
Partner | ||||
Belgium | 10 | 10 | 20506014 | 7209121 |
China | 10 | 10 | 7700931 | 14111163 |
China, Hong Kong SAR | 10 | 10 | 8637173 | 9482458 |
Cyprus | 10 | 10 | 129766 | 101579 |
Denmark | 10 | 10 | 331697 | 335068 |
France | 10 | 10 | 19856738 | 9381762 |
Germany | 10 | 10 | 17966838 | 10828464 |
Hungary | 10 | 10 | 96600 | 71378 |
Ireland | 10 | 10 | 200713266 | 128155891 |
Italy | 10 | 10 | 78354 | 173689 |
Malta | 10 | 10 | 159830 | 129516 |
Netherlands | 10 | 10 | 38625707 | 18018603 |
Poland | 10 | 10 | 409739 | 106659 |
Portugal | 10 | 10 | 25916 | 25855 |
Spain | 10 | 10 | 1212545 | 1229172 |
Sweden | 10 | 10 | 45396 | 34972 |
Whilst in 2014 these regular customers represented 73% of total UK exports in 2015 it increased to 75%.
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.
sum | |||
---|---|---|---|
Netflow | |||
Year | 2014 | All | |
Trade Flow | Exports | Imports | |
Partner | |||
Belgium | 20506014 | -26762776 | -6256762 |
Czech Rep. | 78055 | -358822 | -280767 |
Denmark | 331697 | -11743192 | -11411495 |
Finland | 721216 | -12 | 721204 |
France | 19856738 | -43094286 | -23237548 |
Germany | 17966838 | -29234902 | -11268064 |
Greece | 14087 | -7 | 14080 |
Hungary | 96600 | -346 | 96254 |
Ireland | 200713266 | -75342679 | 125370587 |
Italy | 78354 | -542209 | -463855 |
Latvia | 41 | -567 | -526 |
Lithuania | 1216 | -186038 | -184822 |
Luxembourg | 17006 | -23724 | -6718 |
Netherlands | 38625707 | -25742755 | 12882952 |
Poland | 409739 | -4336432 | -3926693 |
Portugal | 25916 | -51332 | -25416 |
Romania | 5829 | -1228494 | -1222665 |
Slovakia | 314 | -90696 | -90382 |
Spain | 1212545 | -6406647 | -5194102 |
Sweden | 45396 | -3107260 | -3061864 |
United Arab Emirates | 1573610 | -15629 | 1557981 |
United States of America | 523017 | -6790 | 516227 |
All | 437806707 | -228279328 | 209527379 |
Year | 2014 | 2015 | All | ||
---|---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports | |
Partner | |||||
All | 437806707 | 228279328 | 265029661 | 156483978 | 1087599674 |
Ireland | 200713266 | 75342679 | 128155891 | 46263897 | 450475733 |
France | 19856738 | 43094286 | 9381762 | 28314091 | 100646877 |
Netherlands | 38625707 | 25742755 | 18018603 | 17658912 | 100045977 |
Germany | 17966838 | 29234902 | 10828464 | 21899123 | 79929327 |
Belgium | 20506014 | 26762776 | 7209121 | 14325697 | 68803608 |
Denmark | 331697 | 11743192 | 335068 | 13681759 | 26091716 |
Spain | 1212545 | 6406647 | 1229172 | 4910615 | 13758979 |
Poland | 409739 | 4336432 | 106659 | 4933917 | 9786747 |
Sweden | 45396 | 3107260 | 34972 | 1962413 | 5150041 |
Year | 2014 | 2015 | |||
---|---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports | |
Month | Milk and cream | ||||
1 | processed | 40215103 | -30423330 | 26195709 | -19770570 |
unprocessed | 46923551 | -21950746 | 26259790 | -14104604 | |
2 | processed | 32298379 | -20614513 | 21025054 | -17405593 |
unprocessed | 40191337 | -18685554 | 25359029 | -12426222 | |
3 | processed | 42987355 | -26335257 | 26293698 | -19060366 |
unprocessed | 43794069 | -17984197 | 26028318 | -12732120 | |
4 | processed | 52900517 | -24770338 | 29029756 | -15331783 |
unprocessed | 42295261 | -19440269 | 25981444 | -15046043 | |
5 | processed | 55987927 | -26409462 | 29415739 | -16511555 |
unprocessed | 40213208 | -21665662 | 29441124 | -14095122 |
Conclusions
The milk and cream trade of the UK for 2014 & 2015 was analyse over the periods January to May in terms of which countries the UK mostly depends on for income (exports) and goods (imports). Over the period, the UK trade surplus fell by 50% form 200 million US dollars in 2014 to just over 100 million US dollars 2015.
Year | 2014 | 2015 | ||
---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports |
Partner | ||||
Ireland | 200713266 | 75342679 | 128155891 | 46263897 |
France | 19856738 | 43094286 | 9381762 | 28314091 |
Germany | 17966838 | 29234902 | 10828464 | 21899123 |
Netherlands | 38625707 | 25742755 | 18018603 | 17658912 |
Belgium | 20506014 | 26762776 | 7209121 | 14325697 |
Ireland remains the UK's number one importer for Milk Products; although exports fell by 73m from 200m in 2014 to 128m in 2015. Whilst imports of Milk products from Ireland also fell during the same period the reduction of 29m from 75m in 2014 to 46m in 2015 was not sufficient to halt decline in the UK Net Balance of trade of 43m with Ireland.
Year | 2014 | 2015 |
---|---|---|
Trade Flow | Exports | Exports |
Partner | ||
Ireland | 200713266 | 128155891 |
Netherlands | 38625707 | 18018603 |
China | 7700931 | 14111163 |
Germany | 17966838 | 10828464 |
China, Hong Kong SAR | 8637173 | 9482458 |
The UK exported to over 100 countries during the period 2014-2015, the num,ner of countries that the UK imported from reduced from 23 to 21, the main ones (top five by trade value) all being geographically close to the UK. China and Hong Kong are the main importers that are not also main exporters.
len | sum | |||
---|---|---|---|---|
Year | 2014 | 2015 | 2014 | 2015 |
Trade Flow | Exports | Exports | Exports | Exports |
Partner | ||||
Belgium | 10 | 10 | 20506014 | 7209121 |
China | 10 | 10 | 7700931 | 14111163 |
China, Hong Kong SAR | 10 | 10 | 8637173 | 9482458 |
Cyprus | 10 | 10 | 129766 | 101579 |
Denmark | 10 | 10 | 331697 | 335068 |
France | 10 | 10 | 19856738 | 9381762 |
Germany | 10 | 10 | 17966838 | 10828464 |
Hungary | 10 | 10 | 96600 | 71378 |
Ireland | 10 | 10 | 200713266 | 128155891 |
Italy | 10 | 10 | 78354 | 173689 |
Malta | 10 | 10 | 159830 | 129516 |
Netherlands | 10 | 10 | 38625707 | 18018603 |
Poland | 10 | 10 | 409739 | 106659 |
Portugal | 10 | 10 | 25916 | 25855 |
Spain | 10 | 10 | 1212545 | 1229172 |
Sweden | 10 | 10 | 45396 | 34972 |
The UK is heavily dependent on its regular customers, the 16 countries (see above) that buy all types of milk and cream every month. They contribute three quarters of the total export value.
Year | 2014 | 2015 | All | ||
---|---|---|---|---|---|
Trade Flow | Exports | Imports | Exports | Imports | |
Partner | |||||
All | 437806707 | 228279328 | 265029661 | 156483978 | 1087599674 |
Ireland | 200713266 | 75342679 | 128155891 | 46263897 | 450475733 |
France | 19856738 | 43094286 | 9381762 | 28314091 | 100646877 |
Netherlands | 38625707 | 25742755 | 18018603 | 17658912 | 100045977 |
Germany | 17966838 | 29234902 | 10828464 | 21899123 | 79929327 |
Belgium | 20506014 | 26762776 | 7209121 | 14325697 | 68803608 |
Denmark | 331697 | 11743192 | 335068 | 13681759 | 26091716 |
Spain | 1212545 | 6406647 | 1229172 | 4910615 | 13758979 |
Poland | 409739 | 4336432 | 106659 | 4933917 | 9786747 |
Sweden | 45396 | 3107260 | 34972 | 1962413 | 5150041 |
United Arab Emirates | 1573610 | 15629 | 1590593 | 27225 | 3207057 |
Romania | 5829 | 1228494 | 36 | 975996 | 2210355 |
United States of America | 523017 | 6790 | 548910 | 46044 | 1124761 |
Italy | 78354 | 542209 | 173689 | 321635 | 1115887 |
Czech Rep. | 78055 | 358822 | 2590 | 486889 | 926356 |
Lithuania | 1216 | 186038 | 1172 | 318407 | 506833 |
Portugal | 25916 | 51332 | 25855 | 308654 | 411757 |
Hungary | 96600 | 346 | 71378 | 4762 | 173086 |
Slovakia | 314 | 90696 | 65 | 39990 | 131065 |
Latvia | 41 | 567 | 567 | 870 | 2045 |
The UK has bi-directional trade (i.e. both exports and imports) with 20 countries, although for some the trade value (in US dollars) is suspiciously low, which raises questions about the data's accuracy.