The brazilian trade data is provided by Ministério da Indústria, Comércio Exterior e Serviços (MDIC) at their official website, where users can download trade data by municipality and state. There is no option to download trade data by Mesoregion.
This repository contains a python code that downloads files from the MDIC website, merges raw municipalities trade data with municipalities names and codes, groups data by Mesoregion and finally outputs .csv files.
There were mismatches for about 1.000 municipalities regarding MDIC and IBGE databases. Through a series of Vlookups and general feature engineering, the spreadsheet named municipalities_lookup.xlsx
contains matching codes for MDIC and IBGE.
First of all, install the requirements via pip install -r requirements.txt
. After that, initiate a ExportsByMesoregion instance with the desired parameters and then call the download_data_and_aggregate_by_meso() method.
ExportsObject = ExportsByMesoregion(start_year=2020, end_year=2020, transaction_type='imports') ExportsObject.download_data_and_aggregate_by_meso()
Output:
2020 - Municipalities imports finished downloading
2020 - Mesoregions info added to municipalities data
2020 - Mesoregions data aggregated
2020 - Mesoregions data saved
Although the main goal of this repo is to consolidate mesoregion trade data, one could use the exp_municip() or imp_municip() functions to consolidate bulk municipality data with municipalities names and UFs, since the files provided by MDIC contains only their code.
Raw data:
After exp_municip()/imp_municip():