This example creates a function to retrieve data from UNCOMTRADE using their public API

In [7]:
def comtrade(reporter,partner,year,freq,commoditycode):
    #Import your libraries
    import json
    import urllib
    import pandas as pd
    
    #Import the index of countries
    partnerurl = 'https://comtrade.un.org/data/cache/partnerAreas.json'
    areas = urllib.request.urlopen(partnerurl)
    data = json.loads(areas.read())
    data = data['results']
    index = {}
    for i in range(len(data)):
        upper = data[i]['text']
        lower = upper.lower()
        index.update({lower: data[i]['id']})
    
    #Retrieve numeric codes for reporter and partner
    reporter = index[str(reporter)]
    partner =  index[str(partner)]
    
    #Set the URL API
    url = 'http://comtrade.un.org/api/get?' + \
        'max=50000&' + \
        'type=C&' + \
        'freq=' + str(freq) + '&' + \
        'px=HS&' + \
        'ps=' + str(year) + '&' + \
        'r=' + reporter + '&' + \
        'p=' + partner + '&' + \
        'rg=all&' + \
        'cc=' + commoditycode + '&' + \
        'fmt=json'
    
    #Import data with the API, transform JSON into a frame
    urlopen = urllib.request.urlopen(url)
    data = json.loads(urlopen.read())
    data = pd.io.json.json_normalize(data['dataset'])
    
    #Return the data
    return data
In [8]:
#Import data from 2000 through 2014
first = 2000
last = 2014

reporter = 'brazil'
partner = 'usa'
freq = 'A'
ccode = 'TOTAL'

for year in list(range(first,last)):
    if year == first:
        frame = comtrade(reporter, partner, year, freq, ccode)
    else:
        framet = comtrade(reporter, partner, year, freq, ccode)
        frame = frame.append(framet)

print(frame)
  AltQuantity CIFValue FOBValue GrossWeight  IsLeaf NetWeight TradeQuantity  \
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   
0        None     None     None        None       0      None          None   
1        None     None     None        None       0      None          None   

    TradeValue  aggrLevel cmdCode  ...  qtAltCode qtAltDesc qtCode  \
0  13037379871          0   TOTAL  ...       None                1   
1  13389888579          0   TOTAL  ...       None                1   
0  13050768718          0   TOTAL  ...       None                1   
1  14398230083          0   TOTAL  ...       None                1   
0  10440316218          0   TOTAL  ...       None                1   
1  15559315191          0   TOTAL  ...       None                1   
0   9731968193          0   TOTAL  ...       None                1   
1  16937180785          0   TOTAL  ...       None                1   
0  11538691153          0   TOTAL  ...       None                1   
1  20403165980          0   TOTAL  ...       None                1   
0  12854779946          0   TOTAL  ...       None                1   
1  22810092517          0   TOTAL  ...       None                1   
0  14856489424          0   TOTAL  ...       None                1   
1  24774417482          0   TOTAL  ...       None                1   
0  18889840237          0   TOTAL  ...       None                1   
1  25335516490          0   TOTAL  ...       None                1   
0  25849679698          0   TOTAL  ...       None                1   
1  27734718957          0   TOTAL  ...       None                1   
0  20214137728          0   TOTAL  ...       None                1   
1  15744930250          0   TOTAL  ...       None                1   
0  27200503124          0   TOTAL  ...       None                1   
1  19240185206          0   TOTAL  ...       None                1   
0  34233525684          0   TOTAL  ...       None                1   
1  25942952810          0   TOTAL  ...       None                1   
0  32607902409          0   TOTAL  ...       None                1   
1  26849876493          0   TOTAL  ...       None                1   
0  36280346302          0   TOTAL  ...       None                1   
1  24865952835          0   TOTAL  ...       None                1   

        qtDesc rgCode  rgDesc  rt3ISO rtCode rtTitle    yr  
0  No Quantity      1  Import     BRA     76  Brazil  2000  
1  No Quantity      2  Export     BRA     76  Brazil  2000  
0  No Quantity      1  Import     BRA     76  Brazil  2001  
1  No Quantity      2  Export     BRA     76  Brazil  2001  
0  No Quantity      1  Import     BRA     76  Brazil  2002  
1  No Quantity      2  Export     BRA     76  Brazil  2002  
0  No Quantity      1  Import     BRA     76  Brazil  2003  
1  No Quantity      2  Export     BRA     76  Brazil  2003  
0  No Quantity      1  Import     BRA     76  Brazil  2004  
1  No Quantity      2  Export     BRA     76  Brazil  2004  
0  No Quantity      1  Import     BRA     76  Brazil  2005  
1  No Quantity      2  Export     BRA     76  Brazil  2005  
0  No Quantity      1  Import     BRA     76  Brazil  2006  
1  No Quantity      2  Export     BRA     76  Brazil  2006  
0  No Quantity      1  Import     BRA     76  Brazil  2007  
1  No Quantity      2  Export     BRA     76  Brazil  2007  
0  No Quantity      1  Import     BRA     76  Brazil  2008  
1  No Quantity      2  Export     BRA     76  Brazil  2008  
0  No Quantity      1  Import     BRA     76  Brazil  2009  
1  No Quantity      2  Export     BRA     76  Brazil  2009  
0  No Quantity      1  Import     BRA     76  Brazil  2010  
1  No Quantity      2  Export     BRA     76  Brazil  2010  
0  No Quantity      1  Import     BRA     76  Brazil  2011  
1  No Quantity      2  Export     BRA     76  Brazil  2011  
0  No Quantity      1  Import     BRA     76  Brazil  2012  
1  No Quantity      2  Export     BRA     76  Brazil  2012  
0  No Quantity      1  Import     BRA     76  Brazil  2013  
1  No Quantity      2  Export     BRA     76  Brazil  2013  

[28 rows x 35 columns]
In [9]:
#Print only the relevant information

print(frame[['yr','ptTitle','rtTitle','rgDesc','TradeValue']])
     yr ptTitle rtTitle  rgDesc   TradeValue
0  2000     USA  Brazil  Import  13037379871
1  2000     USA  Brazil  Export  13389888579
0  2001     USA  Brazil  Import  13050768718
1  2001     USA  Brazil  Export  14398230083
0  2002     USA  Brazil  Import  10440316218
1  2002     USA  Brazil  Export  15559315191
0  2003     USA  Brazil  Import   9731968193
1  2003     USA  Brazil  Export  16937180785
0  2004     USA  Brazil  Import  11538691153
1  2004     USA  Brazil  Export  20403165980
0  2005     USA  Brazil  Import  12854779946
1  2005     USA  Brazil  Export  22810092517
0  2006     USA  Brazil  Import  14856489424
1  2006     USA  Brazil  Export  24774417482
0  2007     USA  Brazil  Import  18889840237
1  2007     USA  Brazil  Export  25335516490
0  2008     USA  Brazil  Import  25849679698
1  2008     USA  Brazil  Export  27734718957
0  2009     USA  Brazil  Import  20214137728
1  2009     USA  Brazil  Export  15744930250
0  2010     USA  Brazil  Import  27200503124
1  2010     USA  Brazil  Export  19240185206
0  2011     USA  Brazil  Import  34233525684
1  2011     USA  Brazil  Export  25942952810
0  2012     USA  Brazil  Import  32607902409
1  2012     USA  Brazil  Export  26849876493
0  2013     USA  Brazil  Import  36280346302
1  2013     USA  Brazil  Export  24865952835