Wednesday, November 18, 2015

Week 4: Creating Graphs for Your Data

Introduction

In this week assignment, the goal is create various chart types for your data of choice. In my case, I working with the gapminder datasets. My previous analysis focus on countries in Sub Sahara Africa region. However, I have decided to use the whole dataset without regards to regional differences for the simple reason that I need a large datasets for this analysis.

Code

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 18 16:02:17 2015

"""

import pandas
import os
import seaborn
import matplotlib.pyplot as plt

DATA_PATH = os.path.join(os.getcwd(), "data")
DATA_FILE = os.path.join(DATA_PATH, "gapminder.csv")
econ_data = pandas.read_csv(DATA_FILE, low_memory=False)

# Extracting data pertinent to variables of interest
econ_data = \
econ_data[['country', 'incomeperperson', 'relectricperperson', 'urbanrate']]

# bug fix for display format to avoid run time errors
pandas.set_option('display.float_format', lambda x: '%f' % x)

# Set pandas to display all columns and rows in DataFrame
pandas.set_option('display.max_rows', None)
pandas.set_option('display.max_columns', None)


# setting variables of interest to numeric
econ_data['incomeperperson'] = \
pandas.to_numeric(econ_data['incomeperperson'], errors='coerce')
econ_data['relectricperperson'] = \
pandas.to_numeric(econ_data['relectricperperson'], errors='coerce')
econ_data['urbanrate'] = \
pandas.to_numeric(econ_data['urbanrate'], errors='coerce')

# Univariate histrogram for quantitative variable - incomeperperson
seaborn.distplot(econ_data['incomeperperson'].dropna(), kde=False)
plt.xlabel('Per Capita Income')
plt.ylabel('Frequency')
plt.title('Global Income distribution')

# Univariate histrogram for quantitative variable - relectricperperson
seaborn.distplot(econ_data['relectricperperson'].dropna(), kde=False)
plt.xlabel('Per Capita kWh')
plt.ylabel('Frequencey')
plt.title('Global Household Electricity Consumption')

# Univariate histrogram for quantitative variable - urbanrate
seaborn.distplot(econ_data['urbanrate'].dropna(), kde=False)
plt.xlabel('Urban Rate')
plt.ylabel('Frequency')
plt.title('Global Urban Rate distribution')

# Splitting per capita income in quartiles
def percapitagdp(row):
if row['incomeperperson'] <= econ_data['incomeperperson'].quantile(.25):
return 'LowGDP'
if row['incomeperperson'] > econ_data['incomeperperson'].quantile(.25) and \
row['incomeperperson'] <= econ_data['incomeperperson'].quantile(.75):
return 'MediumGDP'
if row['incomeperperson'] > econ_data['incomeperperson'].quantile(.75):
return 'HighGDP'

econ_data['percapitagdp'] =\
econ_data.apply(lambda row: percapitagdp(row), axis=1)

dropnas = econ_data['percapitagdp'].value_counts(sort=True, dropna=True)
print(dropnas)

# bivariate bar chart for Household Electricity Consuption and Income Levels C-> Q
seaborn.factorplot(x='percapitagdp', y='relectricperperson',
data=econ_data, kind='bar', ci=None)
plt.xlabel('Income Group')
plt.ylabel('Mean kWh Rate')

# basic scatterplot for incomeperperson vs relectricperperson Q-> Q
seaborn.regplot(x='incomeperperson', y='relectricperperson',
data=econ_data, fit_reg=False)
plt.xlabel('Income Per Capita')
plt.ylabel('Household Electricity Consumption')
plt.title('Scatterplot to show Association between Income Per Capita and \
Electricity Per Capita')


def areatype(row):
if row['urbanrate'] <= econ_data['urbanrate'].quantile(.25):
return 'Village'
if row['urbanrate'] > econ_data['urbanrate'].quantile(.25) and \
row['urbanrate'] <= econ_data['urbanrate'].quantile(.75):
return 'Town'
if row['urbanrate'] > econ_data['urbanrate'].quantile(.75):
return 'City'

econ_data['areatype'] =\
econ_data.apply(lambda row: areatype(row), axis=1)

dropnas = econ_data['areatype'].value_counts(sort=False, dropna=True)
print(dropnas)

# bivariate bar chart for areatype(urbanrate) and relectricperperson C-> Q
seaborn.factorplot(x='areatype', y='relectricperperson',
data=econ_data, kind='bar', ci=None)
plt.ylabel('Household Electric Consumption')
plt.xlabel('Area Classification')

# basic scatterplot for urbanrate vs relectricperperson Q-> Q
seaborn.regplot(x='urbanrate', y='relectricperperson',
data=econ_data, fit_reg=False)
plt.ylabel('Household Electric Consumption')
plt.xlabel('Urban Rate')
plt.title('Scatterplot to show Association between Urban Rate and \

Electricity Per Capita')

Univariate Graph for Per Capita Income

This graph is unimodal and positively skewed with large number of small income earners and small number of large income earners. Thus, potential evidence of pronounced income disparity globally.





Univariate Graph for Per Household Electricity Use

This graph is unimodal with the highest peak at 500 kWh level. Also the chart is positively skewed which is evidenced by this fact the measures of central tendency are in this in this order:  mode < median < mean

From the charts showing income and household electricity consumption, one can surmise that, there seems to be some relationship between household electricity consumption and income levels. The extent of the relation will be further explored.

Univariate Graph for Urban Rate

This graph appears to be bi-modal as evidenced by two sets of peaks at 40 and 70 urban rate levels.
This points to a fact that, globally, one is likely to see small urban and large urban centers across countries.

Bivariate Graph - relectricperperson vs. incomeperperson(Q ->C)


From this chart, evidence exist that high income households do consume more electricity per capita.

Bivariate Graph - relectricperperson vs. incomeperperson(Q ->Q)


This graph shows a positive relationship between income and household electricity consumption.

Bivariate Graph - urbanrate vs. relectricperperson(Q ->C)

From this graph, there is evidence of positive relationship between urban rate and household electricity consumption. Thus, higher urban rates  correlate to higher household electricity consumption.

Bivariate Graph - urbanrate vs. relectricperperson(Q ->Q)

From this graph, one can decipher a positive relationship between urbanrate and relectricperperson.
However, the extent of the relationship may be a weak one.

Thursday, November 12, 2015

Week 3: Making Data Management Decisions

In this week's assignment, I chose to bin my variables of interest - "relectricperperson", "incomeperperson" and "urbanrate" into categories. The basis of this split is percentile.

Code

# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 12:59:21 2015
Week 3 submission
"""

import pandas
import os

DATA_PATH = os.path.join(os.getcwd(), "data")
DATA_FILE = os.path.join(DATA_PATH, "gapminder.csv")
econ_data = pandas.read_csv(DATA_FILE, low_memory=False)

pandas.set_option('display.float_format', lambda x: '%f' % x)

# setting variables of interest to numeric
econ_data['incomeperperson'] = \
pandas.to_numeric(econ_data['incomeperperson'], errors='coerce')
econ_data['relectricperperson'] = \
pandas.to_numeric(econ_data['relectricperperson'], errors='coerce')
econ_data['urbanrate'] = \
pandas.to_numeric(econ_data['urbanrate'], errors='coerce')


# Extracting data pertinent to variables of interest
relevant_data = \
econ_data[['country', 'continent', 'incomeperperson', 'relectricperperson',
'urbanrate']]

# Extract only countries in Sub-Sahara Africa.
# For this addeded a new column - continent to gapminder dataframe
sub_sahara_africa = relevant_data[relevant_data['continent'] == 'Africa']
# print(sub_sahara_africa)

sub_sahara_africa.describe()

# Create a second copy of sub_sahara_africa for further data analysis
sub2_sahara_africa = sub_sahara_africa.copy()
# print(sub2_sahara_africa)


'''
Split income data into 3 strata - LowGDP, MediumGDP, HighGDP
Using percentile values to bin dataset
LowGDP: <= 25 percentile of incomeperperson
MiddleGDP: between 25 percentile and 75 percentile of incomeperperson
HighGDP: greater than 75 percentile of incomeperperson

incomeperperson
count 43.000000
mean 1098.598153
std 1705.541951
min 103.775857
25% 272.888584
50% 411.501447
75% 804.478821
max 8654.536845

'''


def percapitagdp(row):
if row['incomeperperson'] <= 272.888584:
return 'LowGDP'
if row['incomeperperson'] > 272.888584 and \
row['incomeperperson'] <= 804.478821:
return 'MediumGDP'
if row['incomeperperson'] > 804.478821:
return 'HighGDP'


sub2_sahara_africa['percapitagdp'] =\
sub2_sahara_africa.apply(lambda row: percapitagdp(row), axis=1)

# Calculate descriptive statistics for incomeperperson grouped by percapitagdp
per_capitagdp_stats =\
sub2_sahara_africa.groupby('percapitagdp').agg({'incomeperperson':
['count', 'mean',
'std', 'max']})

print(per_capitagdp_stats)


'''
Split relectricperperson data into 3 categories
- LowkWh, MediumkWh, HighkWh
LowkWh: <= 25 percentile of relectricperperson
MiddlekWh: between 25 percentile and 75 percentile of incomeperperson
HighkWh: greater than 75 percentile of incomeperperson

relectricperperson
count 22.000000
mean 149.889484
std 222.403251
min 0.000000
25% 38.325833
50% 57.961848
75% 150.778896
max 920.137600

'''


def percapitakWh(row):
if row['relectricperperson'] <= 38.325833:
return 'LowkWh'
if row['relectricperperson'] > 38.325833 and \
row['relectricperperson'] <= 150.778896:
return 'MediumkWh'
if row['relectricperperson'] > 150.778896:
return 'HighkWh'


sub2_sahara_africa['percapitakWh'] =\
sub2_sahara_africa.apply(lambda row: percapitakWh(row), axis=1)

# Calculate descriptive statistics for relectricperperson grouped by percapitakWh
per_capitakWh_stats =\
sub2_sahara_africa.groupby('percapitakWh').agg({'relectricperperson':
['count', 'mean',
'std', 'max']})
print(per_capitakWh_stats)


'''
Split urbanrate data into 3 categories - rural, town, urban
rural: >= 25 percentile of urbanrate
town: between 25 percentile and 75 percentile of urbanrate
urban: greater than 75 percentile of urbanrate

urbanrate
count 44.000000
mean 39.774091
std 17.063891
min 12.980000
25% 26.390000
50% 37.550000
75% 49.090000
max 87.300000

'''


def areatype(row):
if row['urbanrate'] <= 26.390000:
return 'rural'
if row['urbanrate'] > 26.390000 and \
row['urbanrate'] <= 49.090000:
return 'town'
if row['urbanrate'] > 49.090000:
return 'urban'
sub2_sahara_africa['areatype'] =\
sub2_sahara_africa.apply(lambda row: areatype(row), axis=1)

# Calculate descriptive statistics for areatype across countries
per_capita_areatype_stats =\
sub2_sahara_africa.groupby('areatype').agg({'urbanrate':
['count', 'mean',
'std', 'max']})
print(per_capita_areatype_stats)


Output



                                     incomeperperson                                    
                                  count        mean            std                     max
percapitagdp                                                    
HighGDP                   11    3265.406288    2281.132925   8654.536845
LowGDP                    11   196.132710       55.934473         269.892881
MediumGDP              21   436.323409      120.507293        713.639303

                                relectricperperson                                 
                                count         mean                std                     max
percapitakWh                                                    
HighkWh                    6           425.257250    284.256336        920.137600
LowkWh                     6             22.610565      13.856819          38.222943
MediumkWh              10             61.036175      17.145977          97.246492

                                     urbanrate                              
                                count           mean               std             max
areatype                                        
rural                          11         20.132727      4.027548    25.520000
town                          22         37.951818     5.654132     48.780000
urban                         11         63.060000   11.856559     87.300000

Summary

Per Capita GDP

I split the income levels into the following categories - HighGDP, MediumGDP and LowGDP. 
The top income earners across Sub Sahara Africa countries take home about  32 times that of low income earners. Median Income earners earns approximately 3 times that of the lowest income earners. 

Per Capita Electricity Consumption

I also split electricity consumption per capita into the following categories - HighkWh, LowkWh and MediumkWh. 
Similarly, the picture for per capita electric consumption is no different.  The top consumers of electricity per capital use 24 times more vis-a-vis low per capita electricity consumers. The medium rung of electricity consumers triple that of lowest consumers.

Urbanization

Broadly, I split population centers across 3 groups, viz, rural, town and urban. 
On average more people in Sub Saharan Africa live in urban centers. Approximately, two-thirds of the population live in urban centers.

Sunday, November 8, 2015

Week 2: Preliminary Analysis and Research Question Refinement.

Preliminary data analysis and refinement of research question to focus on Sub-Sahara Africa

After I carefully reviewed the GapMinder dataset, I have decided to focus my research on countries in Sub-Saharan Africa instead. To accomplished this, I added a new column - 'continent' to gapminder.csv and created a new dataframe - 'sub-sahara-africa' by sub-setting the data to only include countries that are in aforementioned region.

Python Code

import pandas
import os

print(os.getcwd())
DATA_PATH = os.path.join(os.getcwd(), "data")
DATA_FILE = os.path.join(DATA_PATH, "gapminder.csv")
econ_data = pandas.read_csv(DATA_FILE, low_memory=False)

pandas.set_option('display.float_format', lambda x: '%f' % x)

# setting variables of interest to numeric
econ_data['incomeperperson'] = \
pandas.to_numeric(econ_data['incomeperperson'], errors='coerce')
econ_data['relectricperperson'] = \
pandas.to_numeric(econ_data['relectricperperson'], errors='coerce')
econ_data['urbanrate'] = \
pandas.to_numeric(econ_data['urbanrate'], errors='coerce')

print('***** Extrating Gapminder Data for variables of interest*****')
relevant_data = \
econ_data[['country', 'continent', 'incomeperperson', 'relectricperperson',
'urbanrate']]

# making a copy of original dataframe
econ2_data = econ_data.copy()

# Extract only countries in Sub-Sahara Africa.
# For this I added a new column - continent to gapminder dataframe
sub_sahara_africa = relevant_data[relevant_data['continent'] == 'Africa']
print(sub_sahara_africa)


print('***** Per Capita Electricity Consumption and relative frequencies*****')
urban_rate = \
sub_sahara_africa['urbanrate'].value_counts(sort=False,
dropna=False,
normalize=False)
print(urban_rate)

rfreq_urban_rate = \
sub_sahara_africa['urbanrate'].value_counts(sort=False, dropna=False,
normalize=True)
print(rfreq_urban_rate)

per_capita_electric = \
sub_sahara_africa['relectricperperson'].value_counts(sort=False,
dropna=False,
normalize=False)
print(per_capita_electric)

rfreq_per_capita_electric = \
sub_sahara_africa['relectricperperson'].value_counts(sort=False,
dropna=False,
normalize=True)
print(rfreq_per_capita_electric)

per_capita_gdp = \
sub_sahara_africa['incomeperperson'].value_counts(sort=False,
dropna=False,
normalize=False)
print(per_capita_gdp)

rfreq_per_capita_gdp = \
sub_sahara_africa['incomeperperson'].value_counts(sort=False,
dropna=False,
normalize=True)
print(rfreq_per_capita_gdp)

# Create a second copy of sub_sahara_africa for further data analysis
sub2_sahara_africa = sub_sahara_africa.copy()

# Create a second copy of sub_sahara_africa for further data analysis
sub2_sahara_africa = sub_sahara_africa.copy()

print('Split income data into 3 strata - LowGDP, MediumGDP, HighGDP')
sub2_sahara_africa['percapitagdp'] =\
pandas.qcut(sub2_sahara_africa.incomeperperson, 3,
labels=["LowGDP", "MediumGDP", "HighGDP"])
print(sub2_sahara_africa)

# Frequencey description of income strata
per_capita_gdp_bin = \
sub2_sahara_africa['percapitagdp'].value_counts(sort=False,
dropna=True)
print(per_capita_gdp_bin)

rfreq_per_capita_gdp_bin = \
sub2_sahara_africa['percapitagdp'].value_counts(sort=False,
dropna=True,
normalize=True)


print(rfreq_per_capita_gdp_bin)

print('Split relectricperperson data into 3 categories \
- LowkWh, MediumkWh, HighkWh')
sub2_sahara_africa['percapitakWh'] =\
pandas.qcut(sub2_sahara_africa.relectricperperson, 3,
labels=["LowkWh", "MediumkWh", "HighkWh"])
print(sub2_sahara_africa)
per_kWh_use = \
sub2_sahara_africa['percapitakWh'].value_counts(sort=False,
dropna=True)
print(per_kWh_use)

rfreq_per_kWh_use = \
sub2_sahara_africa['percapitakWh'].value_counts(sort=False,
dropna=True,
normalize=True)


print(rfreq_per_kWh_use)

print('Split urbanrate data into 3 categories - rural, town, urban')
sub2_sahara_africa['areatype'] =\
pandas.qcut(sub2_sahara_africa.urbanrate, 3,
labels=["rural", "town", "urban"])
print(sub2_sahara_africa)
areatype_count = \
sub2_sahara_africa['areatype'].value_counts(sort=False,
dropna=True)
print(areatype_count)

rfreq_areatype_count = \
sub2_sahara_africa['areatype'].value_counts(sort=False,
dropna=True,
normalize=True)


print(rfreq_areatype_count)

#Aggregating data across groups
sub2_sahara_africa.groupby('percapitagdp').agg({'incomeperperson': [numpy.size, numpy.mean]})
sub2_sahara_africa.groupby('percapitakWh').agg({'relectricperperson': [numpy.size, numpy.mean]})

sub2_sahara_africa.groupby('areatype').agg({'urbanrate': [numpy.size, numpy.mean]})

# Aggregating per capita income groups  across country
by_gdp = sub2_sahara_africa.groupby(['percapitagdp', 'country'])
by_gdp.incomeperperson.mean()

# Aggregating per capita electric consumption groups  across country
by_kWh = sub2_sahara_africa.groupby(['percapitakWh', 'country'])
by_kWh.relectricperperson.mean()



# Aggregating urbanrate  groups  across country
by_areatype = sub2_sahara_africa.groupby(['areatype', 'country'])
by_areatype.urbanrate.mean()

Dataframe Output

***** Extrating Gapminder Data for variables of interest*****
              country  incomeperperson  relectricperperson  urbanrate
4                   Angola      1381.004268          172.999227      56.70
19                   Benin       377.039699           38.222943      41.20
24                Botswana      4189.436587          454.795705      59.58
28            Burkina Faso       276.200413                 NaN      19.56
31                Cameroon       713.639303           59.551245      56.76
33              Cape Verde      1959.844472                 NaN      59.62
35    Central African Rep.       239.518749                 NaN      38.58
36                    Chad       275.884287                 NaN      26.68
42             Congo, Rep.      1253.292015           56.372450      61.34
45           Cote d'Ivoire       591.067944           70.387444      48.78
51                Djibouti       895.318340                 NaN      87.30
57       Equatorial Guinea      8654.536845                 NaN      39.38
58                 Eritrea       131.796207           20.288131      20.72
60                Ethiopia       220.891248           15.056236      17.00
66                   Gabon      4180.765821          537.104738      85.04
67                  Gambia       354.599726                 NaN      56.42
70                   Ghana       358.979540           97.246492      50.02
78                  Guinea       411.501447                 NaN      34.44
79           Guinea-Bissau       161.317137                 NaN      29.84
97                   Kenya       468.696044           41.180003      21.60
106                Lesotho       495.734247                 NaN      25.46
107                Liberia       155.033231                 NaN      60.14
114             Madagascar       242.677534                 NaN      29.52
115                 Malawi       184.141797                 NaN      18.80
118                   Mali       269.892881                 NaN      32.18
122             Mauritania       609.131206                 NaN      41.00
123              Mauritius      5182.143721                 NaN      42.48
131             Mozambique       389.763634           31.386838      36.84
133                Namibia      2667.246710            0.000000      36.84
141                  Niger       180.083376                 NaN      16.54
142                Nigeria       544.599477           74.064241      48.36
160                 Rwanda       338.266391                 NaN      18.34
166  Sao Tome and Principe              NaN                 NaN      60.56
168                Senegal       561.708585           55.794744      42.38
171             Seychelles      8614.120219                 NaN      54.34
172           Sierra Leone       268.331790                 NaN      37.76
177                Somalia              NaN                 NaN      36.52
178           South Africa      3745.649852          920.137600      60.74
181                  Sudan       523.950151           50.892101      43.44
183              Swaziland      1810.230533                 NaN      24.94
189               Tanzania       456.385712           38.634503      25.52
192                   Togo       285.224449           66.238522      42.00
199                 Uganda       377.421113                 NaN      12.98
211                 Zambia       432.226337          168.623031      35.42
212               Zimbabwe       320.771890          297.883200      37.34

# Generate Summary statistics for Sub Sahara Africa
sub_sahara_africa.describe()

Summary Statistics

        incomeperperson  relectricperperson  urbanrate
count        43.000000           21.000000  45.000000
mean       1296.513138          155.564733  40.688889
std        2046.959164          226.257267  17.250389
min         131.796207            0.000000  12.980000
25%         276.042350           38.634503  26.680000
50%         432.226337           59.551245  38.580000
75%        1074.305177          168.623031  54.340000
max        8654.536845          920.137600  87.300000

The hypothesis stipulated earlier was to test if per capita electric consumption(relectricperperson) can be used as a proxy to economic growth(incomeperperson)in Sub-Sahara Africa. 
A review of the summary statistics reveals that, countries with higher per capita electric consumption also tend to have higher per capita income and hence better economic outcome.

On average, 4 in 10 of the population in Sub Sahara Africa live in urban centers. 
Per Capita Electric consumption across Sub Sahara Africa averages 155.56 kWh in 2008 and income per capita average of $1296.51 in constant 2000 USD.


Frequency and Relative Frequency Distributions

Income Classification Frequency

LowGDP       15
MediumGDP    14
HighGDP      14

Income Classification Relative Frequency

LowGDP      0.340909
MediumGDP   0.318182
HighGDP     0.318182

Per Capita Electricity Consumption Frequency

LowkWh       8
MediumkWh    7
HighkWh      7

Per Capita Electricity Consumption Relative Frequency

LowkWh      0.181818
MediumkWh   0.159091
HighkWh     0.159091

Locality Classification Frequency

rural    15
town     14
urban    15

Per Capita Electricity Consumption Relative Frequency

rural   0.340909
town    0.318182
urban   0.340909

Aggregating Per Capita Income over Income groups

                        incomeperperson            
                        size        mean
percapitagdp                            
LowGDP             15.000000  221.036056
MediumGDP          14.000000  435.062293
HighGDP            14.000000 2702.379115


Aggregating Per Capita Electric Consumption over Electric Consumption bands

                          relectricperperson           
                           size       mean
percapitakWh                              
LowkWh                 8.000000  26.934737
MediumkWh              7.000000  61.900107
HighkWh                7.000000 378.398571


Aggregating Urbanization rate over urban type

                 urbanrate          
              size      mean
areatype                    
rural    15.000000 22.645333
town     14.000000 38.118571
urban    15.000000 58.448000

Aggregating per capita income groups  across country 

percapitagdp  country                  mean
LowGDP        Burkina Faso            276.200413
              Central African Rep.    239.518749
              Chad                    275.884287
              Congo, Dem. Rep.        103.775857
              Eritrea                 131.796207
              Ethiopia                220.891248
              Guinea-Bissau           161.317137
              Liberia                 155.033231
              Madagascar              242.677534
              Malawi                  184.141797
              Mali                    269.892881
              Niger                   180.083376
              Sierra Leone            268.331790
              Togo                    285.224449
              Zimbabwe                320.771890
MediumGDP     Benin                   377.039699
              Gambia                  354.599726
              Ghana                   358.979540
              Guinea                  411.501447
              Kenya                   468.696044
              Lesotho                 495.734247
              Mozambique              389.763634
              Nigeria                 544.599477
              Rwanda                  338.266391
              Senegal                 561.708585
              Sudan                   523.950151
              Tanzania                456.385712
              Uganda                  377.421113
              Zambia                  432.226337
HighGDP       Angola                 1381.004268
              Botswana               4189.436587
              Cameroon                713.639303
              Cape Verde             1959.844472
              Congo, Rep.            1253.292015
              Cote d'Ivoire           591.067944
              Djibouti                895.318340
              Equatorial Guinea      8654.536845
              Gabon                  4180.765821
              Mauritania              609.131206
              Mauritius              5182.143721
              Namibia                2667.246710
              South Africa           3745.649852
              Swaziland              1810.230533

Aggregating per capita electric consumption groups  across country

Out[172]: 
percapitakWh            country             mean
LowkWh                    Benin               38.222943
                         Congo, Dem. Rep.    30.709244
                                   Eritrea             20.288131
                                 Ethiopia            15.056236
                                 Kenya               41.180003
                            Mozambique          31.386838
                                Namibia              0.000000
                                Tanzania            38.634503
MediumkWh  
                               Cameroon            59.551245
                               Congo, Rep.         56.372450
                                Cote d'Ivoire       70.387444
                                   Nigeria             74.064241
                                  Senegal             55.794744
                                   Sudan               50.892101
                                   Togo                66.238522
HighkWh      
                                 Angola             172.999227
                               Botswana           454.795705
                                 Gabon              537.104738
                                  Ghana               97.246492
                              South Africa       920.137600
                                Zambia             168.623030
                             Zimbabwe           297.883200

Aggregating urbanrate  groups  across country

areatype  country                 mean
rural     Burkina Faso           19.560000
                 Chad                   26.680000
                  Eritrea                20.720000
                Ethiopia               17.000000
           Guinea-Bissau          29.840000
                Kenya                  21.600000
               Lesotho                25.460000
            Madagascar             29.520000
               Malawi                 18.800000
                  Mali                   32.180000
                 Niger                  16.540000
              Rwanda                 18.340000
             Swaziland              24.940000
              Tanzania               25.520000
              Uganda                 12.980000
town    
                   Benin                  41.200000
          Central African Rep.   38.580000
          Congo, Dem. Rep.       33.960000
           Equatorial Guinea      39.380000
                  Guinea                 34.440000
                Mauritania             41.000000
            Mozambique             36.840000
                Namibia                36.840000
                 Senegal                42.380000
              Sierra Leone           37.760000
                Somalia                36.520000
                  Togo                   42.000000
                Zambia                 35.420000
             Zimbabwe               37.340000
urban    
             Angola                 56.700000
           Botswana               59.580000
           Cameroon              56.760000
          Cape Verde             59.620000
          Congo, Rep.            61.340000
           Cote d'Ivoire          48.780000
             Djibouti               87.300000
             Gabon                  85.040000
            Gambia                 56.420000
             Ghana                  50.020000
              Liberia                60.140000
            Mauritius              42.480000
             Nigeria                48.360000
         South Africa           60.740000
             Sudan                  43.440000

Summary of Analysis

Upon running and generating myriad of summary statistics, one can deduce the following
  • Countries in Sub Sahara Africa with large per capita electricity consumption tend to have higher per capita GDP and are more urbanized.