In [1]:
import numpy as np, requests, pandas as pd, zipfile, StringIO,json

In [3]:
df15=pd.read_csv('https://www.worldenergy.org/csv/index-table/2015').set_index('Country name')

In [4]:
df14=pd.read_csv('https://www.worldenergy.org/csv/index-table/2014').set_index('Country name')

In [5]:
df13=pd.read_csv('https://www.worldenergy.org/csv/index-table/2013').set_index('Country name')

In [6]:
df12=pd.read_csv('https://www.worldenergy.org/csv/index-table/2012').set_index('Country name')

In [7]:
df11=pd.read_csv('https://www.worldenergy.org/csv/index-table/2011').set_index('Country name')

In [8]:
df10=pd.read_csv('https://www.worldenergy.org/csv/index-table/2010').set_index('Country name')

In [9]:
df10.columns


Out[9]:
Index([u'Country slug', u'Country ISO 3166-1 alpha-2', u'Total score 2010',
       u'Total rank 2010', u'Energy security score 2010',
       u'Energy security rank 2010', u'Social equity score 2010',
       u'Social equity rank 2010',
       u'Environmental impact mitigation score 2010',
       u'Environmental impact mitigation rank 2010',
       u'Energy performance score 2010', u'Energy performance rank 2010',
       u'Political strength score 2010', u'Political strength rank 2010',
       u'Societal strength score 2010', u'Societal strength rank 2010',
       u'Economic strength score 2010', u'Economic strength rank 2010',
       u'Contextual performance score 2010',
       u'Contextual performance rank 2010'],
      dtype='object')

In [10]:
df15.loc['Luxembourg']


Out[10]:
Country slug                                  luxembourg
Country ISO 3166-1 alpha-2                            LU
Total score 2015                                  7.1775
Total rank 2015                                       19
Energy security score 2015                          1.93
Energy security rank 2015                            105
Social equity score 2015                            9.84
Social equity rank 2015                                3
Environmental impact mitigation score 2015          7.67
Environmental impact mitigation rank 2015             31
Energy performance score 2015                       4.86
Energy performance rank 2015                          30
Political strength score 2015                       9.45
Political strength rank 2015                           8
Societal strength score 2015                        8.68
Societal strength rank 2015                           18
Economic strength score 2015                        9.68
Economic strength rank 2015                            5
Contextual performance score 2015                 2.3175
Contextual performance rank 2015                       9
Name: Luxembourg, dtype: object

In [11]:
#hdi:e3a
    #edu:energy equity
    #gni:energy affordability
    #le:env susti
#idhi:contextual performance
    #edu:societal strength
    #gni:economic strength
    #le:political strength

In [12]:
pop=pd.read_csv('pop.csv').set_index(['Country','Year'])

In [13]:
#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 [14]:
#load dictionary preformatted for viz type
data3=json.loads(file('data3.json','r').read())

In [38]:
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 [39]:
file('data.json','w').write(json.dumps(data))