Learn Python: How to Access World Trade Statistics

In this article you will learn how to access World Trade Statistics from the UN Comtrade website, followed by loading the data into a pandas dataframe. At the time of writing the method described below works, however, this might change due to the sites 2018 upgrade plan.

Calling World Trade Data

Image result for un world trade organization

World Trade statistics are complied by the UN and can be accessed via the Comtrade website.You are able make data calls using an easy to use GUI. There are various options, for data calling, such as:

Type of product – Goods or Services;

Frequency – Monthly or Annual;

Classification – HS, SITC, BEC;

Period – year;

Trade Flows – Import, Export, Re-import, Re-export;

Reporters/Partners – World or Country.

 

What about Classification?

Most data options are self-explantory apart from ‘ Classification’, which requires some underlying knowlegde. The acronmys aviable stand for:

HS – Harmonised System;

SITC – ‘Standard International Trade Classification;

BEC – ‘Broad Economic Categories’.

The Classification you choose may depend on a variety of factors, such as: use, detail, prefernece and maybe convention if you are working for a company. For example, according to the UN, HS codes are more detailed than SITC, which itself is based on HS. While BEC is based on SITC and for transportable goods based on their major end use. A further discussion is beyond the scope of this article.

Viewing World Trade Data

Once you have decdied what data to call chosse how to display results. Resuts can be downloaded into a csv, viewed online or acessed via the API. There are data limits that can be removed by paying for a subscription fee. Alternatively, the API call can be modified to give more results; the method used here.

Click on ‘View API Call’ and copy the link provided into a Jupyter Notebook. We will modifiy this and use as a URL to load the data into a Pandas dataframe.

Loading into a Pandas Dataframe

This is similar to loading a CSV but we use a URL, obtained by modifying the API call from comtrade website. Your API call will be something like:

‘/api/getmax=500&type=C&freq=M&px=HS&ps=2016&r=826&p=0%2Call&rg=2%2C1&cc=28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38′

There are three things to do to make this Url callable:

1. Add the web adress as a prefix ‘https://comtrade.un.org

2. Change 500 to 5000 or whatever you want. I am not sure if there is a restriction, I think its a bit of a hack…

3. Add ‘&fmt=csv’ as a suffix, giving access to csv version.

After these 3 steps the your modified API should be similar to

https://comtrade.un.org/api/get?max=5000&type=C&freq=M&px=HS&ps=2016&r=826&p=0%2Call&rg=2%2C1&cc=28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38&fmt=csv

Assign the API as an object, such as : URL = ‘https://comtrade.un.org/api/get?…’

Next use URL to populate a Pandas dataframe: importing Pandas as pd folowed by the read_csv method: ‘df = pd.read_csv(URL)’

Final Remarks

I hope you have found this article useful, in a future article I will look at analysing data using Pandas pivot tables. I have also written a report on my own findings from the this project, titled: ‘Chemical and Allied Industries: In- and Out-side the Single Market’.

This post was inspired by the final part of a course ‘Learn to Code for Data Analysis’, hosted on futurelearn.com by Royal Holloway University. It is an excellent course and one I highly recommend for new comers.

Please follow and like us: