Francisca Dias
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:
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:
max=5000 to make sure all data is loaded;
&fmt=csv is added at the end to obtain the data in CSV format.
&r=620 is the ID code for Portugal https://comtrade.un.org/data/cache/reporterAreas.json
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))
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)
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'
cork = pd.read_csv(loc)
cork.head()
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
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.
cork_df = cork_df[cork_df['Partner'] != 'World']
cork_df.head()
How much does the Portugal export and import?
Is its balance positive (exports>imports) or negative?
grouped = cork_df.groupby('Trade Flow')['Trade Value (US$)'].agg(['sum'])
grouped
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
imports = cork_df[cork_df['Trade Flow'] == 'Imports']
grouped_imports= imports.groupby(['Partner'])
totalImports = grouped_imports['Trade Value (US$)'].sum().sort_values(ascending=False)
totalImports.head(5)
Exports
exports = cork_df[cork_df['Trade Flow'] == 'Exports']
grouped_exports= exports.groupby(['Partner'])
totalExports = grouped_exports['Trade Value (US$)'].sum().sort_values(ascending=False)
totalExports.head(5)
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.
# 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
# We are using the variable from above : grouped_exports
regular_costumers = grouped_exports.filter(buysEveryMonth)
regular_costumers.head()
How many countries buy cork from Portugal every month?
len(regular_costumers.Partner.value_counts())
Out of how many?
cork_df.Partner.nunique()
Half of the trading partners buy every month cork from Portugal.
regular_costumers['Trade Value (US$)'].sum()/exports['Trade Value (US$)'].sum()
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?
# 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()
If I remove the missing values denoted by NaN, It will show only those countries that both import and export with Portugal.
countries.dropna()
len(countries)
Change Period to datetime
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
trade_by_month = pd.pivot_table(cork_df, index=['Period'], columns=['Trade Flow'],
values=('Trade Value (US$)'), aggfunc=sum)
trade_by_month.head()
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.