In [1]:
# Import librerie per analisi dati (Pandas) e dati Istat
import os
import pandas as pd
import numpy as np
from IPython.core.display import HTML
import istat
import jsonstat
# cache dir per velocizzare analisi in locale
cache_dir = os.path.abspath(os.path.join("..", "tmp/od_la_grande_fuga", "istat_cached"))
istat.cache_dir(cache_dir)
istat.lang(0) # lingua italiano
dir_df = os.path.join(os.path.abspath(''),'stg')
In [2]:
istat_area_condizioni = istat.area(9)
istat_area_condizioni.datasets()
Out[2]:
In [3]:
# Indicatori di reddito
ds_reddito = istat_area_condizioni.dataset('DCCV_QUINTOREDD')
ds_reddito
Out[3]:
In [4]:
ds_reddito.dimension('Territorio')
Out[4]:
In [5]:
# NORD
spec_ne = {
"Territorio":4
}
spec_no = {
"Territorio":33
}
#SUD
spec_s = {
"Territorio":89
}
In [6]:
def quintili(ds, spec, nome):
c = ds_reddito.getvalues(spec)
ds = c.dataset(0)
df = ds.to_data_frame('Anno')
df.reset_index(level=0, inplace=True)
df = df[['Anno','Quinto di reddito equivalente','Value']]
df.columns = ['Anno','Quintile',nome]
df=df[(df['Quintile']=='primo') |
(df['Quintile']=='quinto')
]
return df
In [7]:
df_s = quintili(ds_reddito,spec_s, 'Sud')
df_ne = quintili(ds_reddito,spec_ne, 'NordE')
df_no = quintili(ds_reddito,spec_no, 'NordO')
In [8]:
df_ne.columns = ['AnnoE','QuintileE','NordE']
df_n = pd.concat([df_no,df_ne],axis=1)
In [9]:
def nord(x):
return round(((x['NordE'] + x['NordO'])/2),1)
df_n['Nord'] = df_n.apply(lambda x: nord(x), axis=1)
df_n = df_n[['Anno','Quintile','Nord']]
In [10]:
# Reshape Nord
df_n1=df_n[(df_n['Quintile']=='primo')]
df_n1=df_n1[['Anno','Nord']]
df_n1.columns = ['Anno','1° Quintile']
df_n5=df_n[(df_n['Quintile']=='quinto')]
df_n5=df_n5[['Anno','Nord']]
df_n5.columns = ['Anno','5° Quintile']
In [11]:
df_n1 = df_n1.set_index(['Anno'])
df_n5 = df_n5.set_index(['Anno'])
In [12]:
df_n = pd.concat([df_n1,df_n5],axis=1)
df_n
Out[12]:
In [13]:
# Reshape Sud
df_s1=df_s[(df_s['Quintile']=='primo')]
df_s1=df_s1[['Anno','Sud']]
df_s1.columns = ['Anno','1° Quintile']
df_s5=df_s[(df_s['Quintile']=='quinto')]
df_s5=df_s5[['Anno','Sud']]
df_s5.columns = ['Anno','5° Quintile']
In [14]:
df_s1 = df_s1.set_index(['Anno'])
df_s5 = df_s5.set_index(['Anno'])
In [15]:
df_s = pd.concat([df_s1,df_s5],axis=1)
df_s
Out[15]:
In [16]:
df_filename = r'df_g7_nord.pkl'
df_fullpath = os.path.join(dir_df, df_filename)
df_n.to_pickle(df_fullpath)
df_filename = r'df_g7_sud.pkl'
df_fullpath = os.path.join(dir_df, df_filename)
df_s.to_pickle(df_fullpath)