Package: world-trade-data
World Trade Statistics (WITS) API in PythonStarThis package is an implementation of theWorld Integrated Trade Solution API.
Use this package to explore theTrade and Tariff Datapublished by theWorld Bank.This python package itself is licenced under the MIT License. Different Terms and Conditions apply to the WITS data itself, please read theFrequently Asked Questionson the WITS website.Quick tutorialInstallationInstall or update theWorld Trade Datapython package withpip install world_trade_data --upgradeGet the list of countries, products, indicatorsSee the outputs of the commands below onGitHub. Or even, open thisREADME.mdas a notebook and run it interactively onBinder!importpandasaspdimportworld_trade_dataaswitspd.set_option('display.max_rows',6)wits.get_countries()wits.get_products()wits.get_indicators()All these methods accept adatasourceargument, which can be any ofwits.DATASOURCESThe nomenclature, and data availability, are accessible withget_nomenclatures()andget_dataavailability().Get the Trade or Tariff dataIndicators are available withget_indicator. Tariff rates can be loaded withget_tariff_reportedandget_tariff_estimated.Working with codes rather than with category namesThe three functions above accept aname_or_idargument that defaults to'name'. Usename_or_id='id'to
get codes rather than full description for products and countries:wits.get_indicator('MPRT-TRD-VL',reporter='usa',year='2017',name_or_id='id')Sample use caseIn the below we show how to collect and plot the Import and Export data for the USA in 2017.To begin with, we request the values for the corresponding import and exports. Here, we use the default value forpartner='wld', and the default value forproduct='all'.usa_imports_2017=wits.get_indicator('MPRT-TRD-VL',reporter='usa',year='2017')usa_exports_2017=wits.get_indicator('XPRT-TRD-VL',reporter='usa',year='2017')usa_imports_2017Now we remove the first levels of the indexusa_imports_2017=usa_imports_2017.loc['Annual'].loc['United States'].loc['World']usa_exports_2017=usa_exports_2017.loc['Annual'].loc['United States'].loc['World']Note that one line in the table gives the value for imports on all products:usa_imports_2017.loc['All Products']In order to avoid double counting, we only look at sectors:products=wits.get_products()sectors=products.loc[(products.grouptype=='Sector')&(products.index!='Total')].productdescription.valuessectorsand make sure that we reproduce well the total:assertpd.np.isclose(usa_imports_2017.loc[sectors].Value.sum(),usa_imports_2017.loc['All Products'].Value)Finally we represent the data using e.g.Plotly's Pie Chartsimportplotly.graph_objectsasgofromplotly.subplotsimportmake_subplotsimports_musd=usa_imports_2017.loc[sectors].Value/1e3exports_musd=usa_exports_2017.loc[sectors].Value/1e3fig=make_subplots(rows=1,cols=2,specs=[[{'type':'domain'},{'type':'domain'}]])fig.add_trace(go.Pie(labels=sectors,values=imports_musd,name="Imports"),1,1)fig.add_trace(go.Pie(labels=sectors,values=exports_musd,name="Exports"),1,2)fig.update_traces(hole=.4,scalegroup='usa',textinfo='label',hovertemplate="%{label}<br>%{value:,.0f}M$<br>%{percent}")fig.update_layout(title_text="Trade Statistics, USA, 2017",annotations=[dict(text='Imports<br>{:.3f}T$'.format(imports_musd.sum()/1e6),x=0.17,y=0.5,font_size=16,showarrow=False),dict(text='Exports<br>{:.3f}T$'.format(exports_musd.sum()/1e6),x=0.83,y=0.5,font_size=16,showarrow=False)])fig.show(renderer='notebook_connected')References & AlternativesThe officialWITS portallet you visualize and download trade and tariff data. And the API implemented in this package is documentedhere.The WITS data can be accessed in R with thetradestatisticslibrary.An alternative way to access the WITS data is to usepandasdmx.