Process Import/Export per country and total csv

Open the Mercasa files and get total and per country list


In [16]:
import matplotlib.pyplot as plt
import matplotlib
import os
import pandas as pd
import numpy as np
%matplotlib inline
matplotlib.style.use('ggplot')
import datetime
import locale
import time
locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')
import glob

In [17]:
product = 'acelga/'
pro = 'production/'
imp = 'import/'
pwd = '/Volumes/Macintosh HD/_GitHub/journey-of-food/data/producto/'
aux = '/Volumes/Macintosh HD/_GitHub/journey-of-food/data/aux/'
dwd = '/Volumes/Macintosh HD/_GitHub/journey-of-food/data/source/'

In [18]:
os.chdir(dwd)
files = os.listdir(dwd)
files


Out[18]:
['FH_EPRODMESK.xlsx',
 'FH_EPRODPAISK.xlsx',
 'FH_EPROVPRODK.xlsx',
 'FH_IPRODMESK.xlsx',
 'FH_IPRODPAIK.xlsx',
 'import_2015.csv',
 '~$FH_IPRODPAIK.xlsx']

In [19]:
data = pd.read_excel('FH_IPRODPAIK.xlsx', sheetname='2015', encoding ='utf-8',skiprows=[2],index_col=0,na_values='x')
data.fillna(0,inplace=True)

In [20]:
data.head()


Out[20]:
ALEMANIA AUSTRIA BÉLGICA BULGARIA CHIPRE CROACIA DINAMARCA ESLOVAQUIA ESLOVENIA ESTONIA ... PAISES BAJOS POLONIA PORTUGAL REINO UNIDO REP. CHECA RUMANIA SUECIA TOTAL UE-27 EXTRE UE TOTAL
ACELGA 4.0 0.0 67.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 60.0 0.0 0.0 0.0 0.0 219.0 31.0 250.0
AJO 128.0 0.0 294.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 178.0 0.0 511.0 35.0 0.0 0.0 0.0 2397.0 3009.0 5406.0
ALCACHOFA 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 6.0 0.0 0.0 0.0 81.0 55.0 136.0
APIO 5.0 0.0 97.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 53.0 0.0 30.0 157.0 0.0 0.0 0.0 397.0 55.0 452.0
BERENJENA 42.0 0.0 419.0 0.0 0.0 0.0 0.0 0.0 10.0 0.0 ... 241.0 67.0 47.0 0.0 25.0 3.0 0.0 1327.0 24.0 1351.0

5 rows × 30 columns


In [21]:
data.drop('%cuota',axis=0,inplace=True)
data.drop('TOTAL HORTALIZAS',axis=0,inplace=True)
data.drop('OTRAS HORTALIZAS',axis=0,inplace=True)
data.drop('OTROS CÍTRICOS',axis=0,inplace=True)
data.drop('OTRAS FRUTAS',axis=0,inplace=True)
data.drop('TOTAL FRUTAS',axis=0,inplace=True)
data.drop('TOTAL F. Y H.',axis=0,inplace=True)
data.drop('TOTAL',axis=1,inplace=True)
data.drop('TOTAL UE-27',axis=1,inplace=True)
data.drop('T. HORTALIZAS',axis=0,inplace=True)
data.drop('* Datos provisionales',axis=0,inplace=True)

In [22]:
data = data.loc[pd.notnull(data.index)]

In [23]:
data.index = [x.lower() for x in data.index.values.tolist()]

In [24]:
data.loc[data.index=='acelga'].transpose().sort_values('acelga',ascending=False).sum()


Out[24]:
acelga    250.0
dtype: float64

In [25]:
for producto in data.index.values:
    datos = data.loc[data.index==producto].transpose().sort_values(producto,ascending=False)
    datos.columns = ['Importado']
    datosSum = pd.DataFrame(datos.sum())
    datosSum.columns = ['Ton']
    datos['Percent'] = datos['Importado']/datos['Importado'].sum()*100.0
    datos.Percent = datos.Percent.round(2)
    datos.index.name = 'Pais'
    datosSum.index.name = 'Total'
    try:
        datos[:3].to_csv(pwd+producto+'/'+producto+'_import_country.csv')
        datosSum.to_csv(pwd+producto+'/'+producto+'_import_total.csv')
    except:
        datos[:3].to_csv(pwd+producto+'_import_country.csv')
        datosSum.to_csv(pwd+producto+'_import_total.csv')

In [26]:
data


Out[26]:
ALEMANIA AUSTRIA BÉLGICA BULGARIA CHIPRE CROACIA DINAMARCA ESLOVAQUIA ESLOVENIA ESTONIA ... LUXEMBURGO MALTA PAISES BAJOS POLONIA PORTUGAL REINO UNIDO REP. CHECA RUMANIA SUECIA EXTRE UE
acelga 4.0 0.0 67.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 60.0 0.0 0.0 0.0 0.0 31.0
ajo 128.0 0.0 294.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 178.0 0.0 511.0 35.0 0.0 0.0 0.0 3009.0
alcachofa 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 1.0 0.0 0.0 6.0 0.0 0.0 0.0 55.0
apio 5.0 0.0 97.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 53.0 0.0 30.0 157.0 0.0 0.0 0.0 55.0
berenjena 42.0 0.0 419.0 0.0 0.0 0.0 0.0 0.0 10.0 0.0 ... 0.0 0.0 241.0 67.0 47.0 0.0 25.0 3.0 0.0 24.0
calabacín 58.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 31.0 0.0 ... 0.0 0.0 20.0 0.0 241.0 53.0 0.0 0.0 0.0 9476.0
calabaza 74.0 1336.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 ... 0.0 0.0 986.0 8.0 842.0 3.0 305.0 0.0 0.0 5556.0
cebolla 793.0 3.0 395.0 0.0 0.0 0.0 23.0 0.0 0.0 0.0 ... 0.0 0.0 8469.0 0.0 8227.0 944.0 187.0 0.0 0.0 15014.0
coles 1084.0 0.0 1417.0 0.0 0.0 0.0 0.0 0.0 39.0 0.0 ... 0.0 0.0 6292.0 99.0 2902.0 1149.0 0.0 1.0 0.0 285.0
endivia y escarola 1181.0 0.0 3316.0 252.0 0.0 3.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 799.0 0.0 27.0 0.0 229.0 0.0 0.0 373.0
espárrago 3.0 0.0 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 136.0 0.0 6.0 0.0 0.0 0.0 0.0 8642.0
espinaca 24.0 0.0 26.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 81.0 0.0 202.0 64.0 0.0 0.0 0.0 19.0
guisante 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 4.0 0.0 15.0 12.0 0.0 0.0 77.0 177.0
judía 11.0 0.0 414.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 62.0 0.0 183.0 30.0 0.0 2.0 26.0 78576.0
lechuga 1059.0 4.0 2491.0 0.0 0.0 0.0 0.0 0.0 27.0 0.0 ... 0.0 0.0 2183.0 48.0 2896.0 434.0 0.0 24.0 11.0 382.0
maíz dulce 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 159.0 0.0 750.0 0.0 0.0 0.0 0.0 5010.0
patata 2652.0 0.0 18703.0 0.0 280.0 0.0 4231.0 0.0 0.0 0.0 ... 2633.0 40.0 55587.0 55.0 13754.0 67115.0 0.0 0.0 878.0 12547.0
pepino 573.0 0.0 297.0 0.0 0.0 0.0 0.0 0.0 28.0 0.0 ... 0.0 0.0 390.0 80.0 127.0 90.0 56.0 0.0 0.0 5230.0
pimiento 54.0 0.0 593.0 2.0 0.0 0.0 0.0 0.0 76.0 0.0 ... 0.0 0.0 692.0 96.0 237.0 25.0 35.0 46.0 0.0 28985.0
puerro 177.0 1.0 9966.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 ... 0.0 0.0 1155.0 0.0 1809.0 11.0 0.0 0.0 0.0 862.0
tomate 2177.0 1.0 6501.0 3.0 62.0 0.0 59.0 0.0 119.0 0.0 ... 0.0 0.0 28655.0 7249.0 84155.0 1009.0 223.0 525.0 0.0 29216.0
zanahoria y nabo 182.0 0.0 920.0 0.0 0.0 0.0 22.0 0.0 0.0 0.0 ... 0.0 0.0 370.0 0.0 856.0 191.0 0.0 0.0 0.0 1092.0
aguacate 47.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 1271.0 0.0 973.0 557.0 0.0 0.0 1.0 56649.0
albaricoque 202.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 12.0 0.0 ... 0.0 0.0 7.0 0.0 94.0 97.0 0.0 0.0 0.0 210.0
arándano 82.0 0.0 7.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 344.0 0.0 83.0 36.0 0.0 7.0 2.0 6325.0
caqui 88.0 0.0 0.0 0.0 7.0 0.0 0.0 0.0 20.0 0.0 ... 0.0 0.0 2.0 0.0 111.0 0.0 0.0 0.0 0.0 134.0
cereza y guinda 84.0 0.0 111.0 0.0 0.0 0.0 163.0 0.0 5.0 0.0 ... 0.0 0.0 52.0 0.0 37.0 0.0 0.0 0.0 0.0 1081.0
ciruela 29.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 412.0 0.0 1102.0 98.0 0.0 0.0 0.0 3717.0
frambuesa 11.0 0.0 2.0 0.0 0.0 0.0 548.0 0.0 0.0 0.0 ... 0.0 0.0 145.0 0.0 709.0 3.0 0.0 0.0 1.0 5701.0
fresa 35.0 0.0 763.0 0.0 0.0 1.0 0.0 0.0 6.0 0.0 ... 0.0 0.0 61.0 0.0 2347.0 4.0 4.0 0.0 0.0 8048.0
grosella 0.0 0.0 6.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 31.0 0.0 5.0 0.0 0.0 0.0 6.0 19.0
higo 19.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 5.0 0.0 ... 0.0 0.0 0.0 0.0 14.0 0.0 0.0 0.0 0.0 29.0
kiwi 240.0 0.0 31658.0 0.0 0.0 0.0 0.0 0.0 144.0 0.0 ... 0.0 0.0 647.0 0.0 10643.0 853.0 0.0 21.0 0.0 55380.0
limón 507.0 0.0 163.0 46.0 0.0 0.0 0.0 6.0 218.0 0.0 ... 6.0 192.0 2527.0 44.0 947.0 116.0 50.0 604.0 0.0 39440.0
mandarina 1565.0 0.0 0.0 0.0 160.0 47.0 22.0 0.0 133.0 0.0 ... 0.0 0.0 1883.0 32.0 6407.0 79.0 101.0 0.0 0.0 2827.0
mango, guayaba 46.0 0.0 44.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 826.0 0.0 652.0 4.0 0.0 0.0 5.0 34113.0
manzana 1132.0 3406.0 5017.0 40.0 0.0 0.0 0.0 0.0 2.0 0.0 ... 0.0 0.0 1328.0 3165.0 19447.0 8374.0 0.0 0.0 0.0 14740.0
melocotón 58.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 22.0 0.0 ... 0.0 0.0 83.0 32.0 131.0 270.0 5.0 0.0 0.0 696.0
melón 197.0 14.0 7.0 0.0 0.0 0.0 0.0 0.0 18.0 0.0 ... 0.0 0.0 3345.0 0.0 568.0 0.0 16.0 0.0 1.0 67760.0
mora 1.0 0.0 2.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 25.0 0.0 2.0 25.0 0.0 0.0 2.0 238.0
naranja 1315.0 0.0 75.0 0.0 0.0 0.0 22.0 0.0 43.0 0.0 ... 0.0 0.0 7249.0 7.0 31689.0 12.0 146.0 0.0 0.0 76354.0
nectarina 239.0 0.0 18.0 0.0 0.0 0.0 6.0 0.0 10.0 0.0 ... 0.0 0.0 41.0 72.0 4562.0 1056.0 25.0 0.0 0.0 1154.0
pera 87.0 0.0 16605.0 6.0 0.0 0.0 0.0 0.0 37.0 0.0 ... 0.0 0.0 9887.0 0.0 12996.0 31.0 0.0 0.0 0.0 8220.0
piña 1505.0 0.0 296.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 ... 0.0 0.0 3499.0 22.0 1977.0 1.0 0.0 0.0 3.0 131574.0
plátano 80.0 0.0 856.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 188.0 0.0 952.0 0.0 0.0 0.0 1.0 216625.0
pomelo 539.0 7.0 2.0 0.0 0.0 0.0 6.0 32.0 0.0 0.0 ... 0.0 0.0 1114.0 31.0 1077.0 70.0 30.0 1.0 7.0 2859.0
sandía 424.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 17.0 0.0 ... 0.0 0.0 1178.0 0.0 548.0 12.0 29.0 0.0 0.0 42614.0
uva de mesa 998.0 0.0 4.0 3.0 0.0 0.0 0.0 0.0 13.0 0.0 ... 0.0 0.0 2522.0 40.0 908.0 2116.0 2.0 0.0 0.0 17744.0

48 rows × 28 columns


In [27]:
names = pd.read_csv(aux+'map_names_import.csv',index_col=1)

In [390]:


In [ ]: