Cork Trade in Portugal

Francisca Dias

Table of Contents

Cork is considered as "the Jewel" of Portuguese Economy.

Therefore I will analyse all imports and exports values during 2016 from The United Nations Comtrade database.

Questions I would like to see answered:

  • How much does Portugal export and import? Its balance is positive or negative?
  • What are the main trading partners, i.e. from/to which countries does Portugal import/export the most?
  • Which are the regular customers, i.e. which countries buy cork from Portugal every month?
  • Which countries does Portugal both import from and export to?

This dataset was obtained from the United Nations Comtrade, by doing the following :

Go the website, type the data you want and on "View API Call" URL, it will show a link similar to this:

/api/get?max=50000&type=C&freq=M&px=HS&ps=2016&r=620&p=all&rg=all&cc=450410

You can modified it by doing the following:

You can also access the data in JSON view by copying the following URL:

This dataset has the following features:

  • Type of Product: goods

  • Frequency: monthly

  • Periods: from January 2016 till December 2016

  • Reporter: Portugal

  • Partners: all

  • Flows: Imports and Exports

  • HS commodity codes: 450410 (Cork; blocks, plates, sheets and strip, tiles of any shape, solid cylinders (including discs), of agglomerated cork (with or without a binding substance))

In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('fivethirtyeight')
cmap = sns.diverging_palette(220, 15, as_cmap=True)
In [2]:
loc = 'http://comtrade.un.org/api/get?max=50000&type=C&freq=M&px=HS&ps=2016&r=620&p=all&rg=all&cc=450410&fmt=csv'
In [3]:
cork = pd.read_csv(loc)
In [4]:
cork.head()
Out[4]:
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
0 HS 2016 201601 January 2016 6 1 1 Imports 620 Portugal ... NaN NaN NaN NaN NaN NaN 509463 NaN NaN 0
1 HS 2016 201601 January 2016 6 1 2 Exports 620 Portugal ... NaN NaN NaN NaN NaN NaN 36007351 NaN NaN 0
2 HS 2016 201601 January 2016 6 1 2 Exports 620 Portugal ... NaN NaN NaN NaN NaN NaN 10805 NaN NaN 0
3 HS 2016 201601 January 2016 6 1 2 Exports 620 Portugal ... NaN NaN NaN NaN 46280.0 NaN 359678 NaN NaN 0
4 HS 2016 201601 January 2016 6 1 2 Exports 620 Portugal ... NaN NaN NaN NaN 39837.0 NaN 315794 NaN NaN 0

5 rows × 35 columns

There are 1078 rows and 35 features.

At this point, if you want you can download the CSV file and open with Excel to view the data in your local machine just type the following command : cork.to_csv('port_cork_2016_monthly.csv', index=False)

I will use just a few columns on my analysis.

Below I will select 4 columns and build a new dataset called cork_df

In [5]:
cols_use = ['Period', 'Partner', 'Trade Flow', 'Trade Value (US$)']
cork_df = cork[cols_use]

This dataset contains the total imports and exports per month, under the 'World' partner.

Since I only want to see per-country data, I will remove the 'World' under the partner feature.

In [6]:
cork_df = cork_df[cork_df['Partner'] != 'World']
cork_df.head()
Out[6]:
Period Partner Trade Flow Trade Value (US$)
2 201601 Angola Exports 10805
3 201601 Argentina Exports 359678
4 201601 Australia Exports 315794
5 201601 Austria Exports 330487
6 201601 Bahrain Exports 1466

How much does the Portugal export and import?

Is its balance positive (exports>imports) or negative?

In [7]:
grouped = cork_df.groupby('Trade Flow')['Trade Value (US$)'].agg(['sum'])
grouped
Out[7]:
sum
Trade Flow
Exports 481758764
Imports 7005680
  • During the 2016 year, almost 482 Million US dollars in total exports.
  • Imports only total 7 million.
  • Balance is positive, by 475 Million dollars
In [8]:
cmap = sns.diverging_palette(240, 128, as_cmap=True)
product_size = cork_df.groupby(['Trade Flow']).size()
ax = product_size.plot.pie(y='Trade Value (US$)', figsize=(8, 8), colormap=cmap, autopct='%1.0f%%',pctdistance=0.5, labeldistance=1.2)
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, bbox_to_anchor=(1.3, 0.8), loc=2,  borderaxespad=0., fontsize=20)
plt.ylabel(' ')
plt.title('Proportion of Cork Exports and Imports in Portugal, 2016', fontsize=20)
plt.show();

What are the main trading partners, i.e. from/to which countries does Portugal import/export the most?

Imports

In [9]:
imports = cork_df[cork_df['Trade Flow'] == 'Imports']
In [10]:
grouped_imports= imports.groupby(['Partner'])
In [11]:
totalImports = grouped_imports['Trade Value (US$)'].sum().sort_values(ascending=False)
In [12]:
totalImports.head(5)
Out[12]:
Partner
Spain                       4760387
Italy                        579803
France                       366745
Chile                        313390
United States of America     278800
Name: Trade Value (US$), dtype: int64

Exports

In [13]:
exports = cork_df[cork_df['Trade Flow'] == 'Exports']
In [14]:
grouped_exports= exports.groupby(['Partner'])
In [15]:
totalExports = grouped_exports['Trade Value (US$)'].sum().sort_values(ascending=False)
In [16]:
totalExports.head(5)
Out[16]:
Partner
France                      76370217
United States of America    73930497
Italy                       64211803
Germany                     60207500
Spain                       35600308
Name: Trade Value (US$), dtype: int64

Which are the regular customers, i.e. which countries buy cork from Portugal every month?

Here we will define regular as a costumer who buys cork from Portugal every month, during the year 2016.

In [17]:
# For countries that trade, or in this case, that buy from Portugal each month
# they should have each 12 rows, one import row for each month
# therefore the following function looks for countries that appear 12 times as import

def buysEveryMonth(group):
    return len(group) == 12
In [18]:
# We are using the variable from above : grouped_exports
regular_costumers = grouped_exports.filter(buysEveryMonth)
In [19]:
regular_costumers.head()
Out[19]:
Period Partner Trade Flow Trade Value (US$)
3 201601 Argentina Exports 359678
4 201601 Australia Exports 315794
5 201601 Austria Exports 330487
8 201601 Belgium Exports 581191
9 201601 Brazil Exports 138588

How many countries buy cork from Portugal every month?

In [20]:
len(regular_costumers.Partner.value_counts())
Out[20]:
53

Out of how many?

In [21]:
cork_df.Partner.nunique()
Out[21]:
109

Half of the trading partners buy every month cork from Portugal.

In [22]:
regular_costumers['Trade Value (US$)'].sum()/exports['Trade Value (US$)'].sum()
Out[22]:
0.9827683902809083

Just over 98% of the total Portugal exports are due to these 52 regular customers.

Which countries does Portugal both import from and export to?

In [23]:
# I will use a pivot table to list the total exports and imports values for each country. 

countries = pd.pivot_table(cork, index=['Partner'], columns=['Trade Flow'], 
                        values=('Trade Value (US$)'), aggfunc=sum)
countries.head()
Out[23]:
Trade Flow Exports Imports
Partner
Algeria 212006.0 NaN
Angola 31294.0 NaN
Argentina 3579230.0 471.0
Armenia 275389.0 NaN
Australia 8015691.0 17612.0

If I remove the missing values denoted by NaN, It will show only those countries that both import and export with Portugal.

In [24]:
countries.dropna()
Out[24]:
Trade Flow Exports Imports
Partner
Argentina 3579230.0 471.0
Australia 8015691.0 17612.0
Austria 4021172.0 84291.0
Belgium 7600539.0 1400.0
Brazil 2307792.0 27.0
Canada 7070522.0 8683.0
Chile 8753906.0 313390.0
China 16561210.0 20545.0
China, Hong Kong SAR 549567.0 39580.0
Czech Rep. 3699553.0 3122.0
Denmark 6065287.0 10451.0
France 76370217.0 366745.0
Georgia 2656194.0 22105.0
Germany 60207500.0 164480.0
Indonesia 71876.0 10851.0
Italy 64211803.0 579803.0
Netherlands 6849388.0 44417.0
Norway 808071.0 50780.0
Poland 6578771.0 157529.0
Rep. of Korea 2361345.0 25691.0
Rep. of Moldova 1521955.0 38487.0
Romania 970267.0 107.0
South Africa 3120840.0 3764.0
Spain 35600308.0 4760387.0
Switzerland 4935780.0 692.0
United Kingdom 6826058.0 1470.0
United States of America 73930497.0 278800.0
World 481758760.0 7005679.0
In [25]:
len(countries)
Out[25]:
110

Change Period to datetime

In [26]:
cork_df['Period']= pd.to_datetime(cork_df['Period'], format='%Y%m', errors='ignore')

Use a pivot table to list the total exports and imports values for each country

In [27]:
trade_by_month = pd.pivot_table(cork_df, index=['Period'], columns=['Trade Flow'], 
                        values=('Trade Value (US$)'), aggfunc=sum)
trade_by_month.head()
Out[27]:
Trade Flow Exports Imports
Period
2016-01-01 36007351 509465
2016-02-01 39197690 509078
2016-03-01 42108701 653457
2016-04-01 44068864 710918
2016-05-01 43202475 405753
In [28]:
trade_by_month.plot(color=sns.color_palette('Set2',12), figsize=(15,8))
fig=plt.gcf()
plt.title('Portuguese Cork Trend 2016', fontsize=20)
plt.ylabel('US Dollars')
plt.xlabel('Months')
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
fig.set_size_inches(18,6)
plt.show();

The cork trade in Portugal from January to December 2016 was analysed in terms of which countries Portugal mostly depends on for income (exports).

Over the period, Portugal had a trade surplus of over 475 Million dollars. France and the USA are the main partners.

Portugal exported to over 109 countries during the year. From these 109 countries, 53 of them were regular buyers, that is, they bought cork from Portugal every month.

Portugal only imported from 27 countries, the main ones being Spain, Italy and France, probably because they are geographically closer.

In [ ]: