In [3]:
import numpy as np, requests, pandas as pd, zipfile, json
In [21]:
df15=pd.read_csv('https://www.worldenergy.org/csv/index-table/2015').set_index('Country name')
In [22]:
df14=pd.read_csv('https://www.worldenergy.org/csv/index-table/2014').set_index('Country name')
In [23]:
df13=pd.read_csv('https://www.worldenergy.org/csv/index-table/2013').set_index('Country name')
In [24]:
df12=pd.read_csv('https://www.worldenergy.org/csv/index-table/2012').set_index('Country name')
In [25]:
df11=pd.read_csv('https://www.worldenergy.org/csv/index-table/2011').set_index('Country name')
In [26]:
df10=pd.read_csv('https://www.worldenergy.org/csv/index-table/2010').set_index('Country name')
In [27]:
df10.columns
Out[27]:
In [28]:
df15.loc['Luxembourg']
Out[28]:
In [29]:
#hdi:e3a
#edu:energy equity
#gni:energy affordability
#le:env susti
#idhi:contextual performance
#edu:societal strength
#gni:economic strength
#le:political strength
In [30]:
pop=pd.read_csv('pop.csv').set_index(['Country','Year'])
In [31]:
#country name converter
def cnc(c):
if c==u'Democratic Republic of the Congo':return 'Congo (Dem. Rep.)'
elif c==u'Ivory Coast':return "C\xc3\xb4te d'Ivoire"
return c
def cnc2(c):
if c==u'Ivory Coast':return "C\xc3\xb4te d'Ivoire"
elif c=="Syria":return 'Syrian Arab Republic'
elif c=="Moldova":return 'Republic of Moldova'
elif c=="South Korea":return 'Republic of Korea'
elif c=="Hong Kong":return 'China, Hong Kong Special Administrative Region'
elif c=="Vietnam":return 'Viet Nam'
elif c=="Tanzania":return 'United Republic of Tanzania'
elif c=="Iran":return 'Iran (Islamic Republic of)'
elif c=="United States":return 'United States of America'
elif c=="Russia":return 'Russian Federation'
elif c=="Libya":return 'Libyan Arab Jamahiriya'
elif c=="Venezuela":return 'Venezuela (Bolivarian Republic of)'
elif c=="Yemen":return 'Yemen '
elif c=="Macedonia":return 'The former Yugoslav Republic of Macedonia'
return c
In [34]:
#load dictionary preformatted for viz type
data3=json.loads(open('data3.json','r').read())
In [35]:
data={}
for d in data3:
if d not in data: data[d]={}
if 'e3' not in data[d]:data[d]['e3']={}
if 'cp' not in data[d]:data[d]['cp']={}
if 'name' not in data[d]:data[d]['name']=data3[d]['name']
if 'code' not in data[d]:data[d]['code']=data3[d]['code']
if 'population' not in data[d]:data[d]['population']={}
for y in range(2010,2017):
if repr(y) not in data[d]['e3']:data[d]['e3'][repr(y)]={u'edu': u'NaN', u'gni': u'NaN', u'le': u'NaN'}
if repr(y) not in data[d]['cp']:data[d]['cp'][repr(y)]={u'edu': u'NaN', u'gni': u'NaN', u'le': u'NaN'}
try:
data[d]['population'][repr(y)]=str(pop.loc[cnc2(data3[d]['name'])].loc[y]['Value']*1000)
#print "success population",y
except: pass
try:
data[d]['e3']['2010']['gni']=str(np.round(df10.loc[cnc(data3[d]['name'])]['Social equity score 2010']/10.0,3))
data[d]['e3']['2010']['edu']=str(np.round(df10.loc[cnc(data3[d]['name'])]['Energy security score 2010']/10.0,3))
data[d]['e3']['2010']['le']=str(np.round(df10.loc[cnc(data3[d]['name'])]['Environmental impact mitigation score 2010']/10.0,3))
data[d]['cp']['2010']['edu']=str(np.round(df10.loc[cnc(data3[d]['name'])]['Societal strength score 2010']/10.0,3))
data[d]['cp']['2010']['gni']=str(np.round(df10.loc[cnc(data3[d]['name'])]['Economic strength score 2010']/10.0,3))
data[d]['cp']['2010']['le']=str(np.round(df10.loc[cnc(data3[d]['name'])]['Political strength score 2010']/10.0,3))
#print "success 2010"
except:pass
try:
data[d]['e3']['2011']['gni']=str(np.round(df11.loc[cnc(data3[d]['name'])]['Social equity score 2011']/10.0,3))
data[d]['e3']['2011']['edu']=str(np.round(df11.loc[cnc(data3[d]['name'])]['Energy security score 2011']/10.0,3))
data[d]['e3']['2011']['le']=str(np.round(df11.loc[cnc(data3[d]['name'])]['Environmental impact mitigation score 2011']/10.0,3))
data[d]['cp']['2011']['edu']=str(np.round(df11.loc[cnc(data3[d]['name'])]['Societal strength score 2011']/10.0,3))
data[d]['cp']['2011']['gni']=str(np.round(df11.loc[cnc(data3[d]['name'])]['Economic strength score 2011']/10.0,3))
data[d]['cp']['2011']['le']=str(np.round(df11.loc[cnc(data3[d]['name'])]['Political strength score 2011']/10.0,3))
#print "success 2011"
except:pass
try:
data[d]['e3']['2012']['gni']=str(np.round(df12.loc[cnc(data3[d]['name'])]['Social equity score 2012']/10.0,3))
data[d]['e3']['2012']['edu']=str(np.round(df12.loc[cnc(data3[d]['name'])]['Energy security score 2012']/10.0,3))
data[d]['e3']['2012']['le']=str(np.round(df12.loc[cnc(data3[d]['name'])]['Environmental impact mitigation score 2012']/10.0,3))
data[d]['cp']['2012']['edu']=str(np.round(df12.loc[cnc(data3[d]['name'])]['Societal strength score 2012']/10.0,3))
data[d]['cp']['2012']['gni']=str(np.round(df12.loc[cnc(data3[d]['name'])]['Economic strength score 2012']/10.0,3))
data[d]['cp']['2012']['le']=str(np.round(df12.loc[cnc(data3[d]['name'])]['Political strength score 2012']/10.0,3))
#print "success 2012"
except:pass
try:
data[d]['e3']['2013']['gni']=str(np.round(df13.loc[cnc(data3[d]['name'])]['Social equity score 2013']/10.0,3))
data[d]['e3']['2013']['edu']=str(np.round(df13.loc[cnc(data3[d]['name'])]['Energy security score 2013']/10.0,3))
data[d]['e3']['2013']['le']=str(np.round(df13.loc[cnc(data3[d]['name'])]['Environmental impact mitigation score 2013']/10.0,3))
data[d]['cp']['2013']['edu']=str(np.round(df13.loc[cnc(data3[d]['name'])]['Societal strength score 2013']/10.0,3))
data[d]['cp']['2013']['gni']=str(np.round(df13.loc[cnc(data3[d]['name'])]['Economic strength score 2013']/10.0,3))
data[d]['cp']['2013']['le']=str(np.round(df13.loc[cnc(data3[d]['name'])]['Political strength score 2013']/10.0,3))
#print "success 2013"
except:pass
try:
data[d]['e3']['2014']['gni']=str(np.round(df14.loc[cnc(data3[d]['name'])]['Social equity score 2014']/10.0,3))
data[d]['e3']['2014']['edu']=str(np.round(df14.loc[cnc(data3[d]['name'])]['Energy security score 2014']/10.0,3))
data[d]['e3']['2014']['le']=str(np.round(df14.loc[cnc(data3[d]['name'])]['Environmental impact mitigation score 2014']/10.0,3))
data[d]['cp']['2014']['edu']=str(np.round(df14.loc[cnc(data3[d]['name'])]['Societal strength score 2014']/10.0,3))
data[d]['cp']['2014']['gni']=str(np.round(df14.loc[cnc(data3[d]['name'])]['Economic strength score 2014']/10.0,3))
data[d]['cp']['2014']['le']=str(np.round(df14.loc[cnc(data3[d]['name'])]['Political strength score 2014']/10.0,3))
#print "success 2014"
except:pass
try:
data[d]['e3']['2015']['gni']=str(np.round(df15.loc[cnc(data3[d]['name'])]['Social equity score 2015']/10.0,3))
data[d]['e3']['2015']['edu']=str(np.round(df15.loc[cnc(data3[d]['name'])]['Energy security score 2015']/10.0,3))
data[d]['e3']['2015']['le']=str(np.round(df15.loc[cnc(data3[d]['name'])]['Environmental impact mitigation score 2015']/10.0,3))
data[d]['cp']['2015']['edu']=str(np.round(df15.loc[cnc(data3[d]['name'])]['Societal strength score 2015']/10.0,3))
data[d]['cp']['2015']['gni']=str(np.round(df15.loc[cnc(data3[d]['name'])]['Economic strength score 2015']/10.0,3))
data[d]['cp']['2015']['le']=str(np.round(df15.loc[cnc(data3[d]['name'])]['Political strength score 2015']/10.0,3))
#print "success 2015"
except:pass
In [36]:
open('data-2018.json','w').write(json.dumps(data))
Out[36]: