In this assignment, I continue with the program used in assignment 3. In the first half of the python code, I use the gapminder data set to look at democratic lower middle-income countries. Democratic countries are defined as having positive ( greater than or equal to 1) polity score. Lower middle-income countries have $1026 < income per person < $4036. I use this data set to look at the important statistics pertaining to democratic score, which is a new variable derived from polity score. The democratic score ranges from 10 to 100 for democratic countries, with higher values denoting more democracy. In the second half of the python code, I look at univariate and bivariate plots that map the relationship between (i) income per person and urban rate, (ii) income per person and internet use rate, (iii) income per person and hiv rate. The code and output are appended below. The first part of the Python code is below:
--------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 30 10:50:43 2015; modified on Sat Jun 17, 2017
@author: ldierker; modified by Sudhir
"""
import pandas
import numpy
import seaborn
import matplotlib.pyplot as plt
# any additional libraries would be imported here
data = pandas.read_csv('SudhirCodeBook.csv', low_memory=False)
print ('_____________________________________________________')
print ('OUTPUT PRINTOUT STARTS HERE')
print ('Total Number of Countries')
print (len(data)) #number of observations (rows)
print ('Total Number of Variables')
print (len(data.columns)) # number of variables (columns)
#setting variables you will be working with to numeric
print ('The Variables in this Database:')
print('(1) country, (2) incomeperperson, (3) polityscore')
print('(4) femaleemployeerate, (5) internetuserate, and (6) urbanrate')
data['incomeperperson'] = data['incomeperperson'].convert_objects(convert_numeric=True)
data['polityscore'] = data['polityscore'].convert_objects(convert_numeric=True)
data['femaleemployrate'] = data['femaleemployrate'].convert_objects(convert_numeric=True)
data['internetuserate'] = data['internetuserate'].convert_objects(convert_numeric=True)
data['urbanrate'] = data['urbanrate'].convert_objects(convert_numeric=True)
#counts and percentages (i.e. frequency distributions) for each variable)
print ('Examples of Sorting by Frequency and by Values')
print ('List Polity Scores for Entire Data Set, Sorted by Frequency')
c2 = data['polityscore'].value_counts(sort=True)
print(c2)
print ('List Polity Scores for Entire Data Set, sorted by Values')
ct2= data.groupby('polityscore').size().sort_index(ascending=True)
print (ct2)
########################################################################################
# REST OF ASSIGNMENT FOCUSES ON DEMOCRATIC (Polity Score > 0 )
# LOWER MIDDLE-INCOME COUNTRIES ($1025 < GNI < $4036)
#
#For the current 2017 fiscal year, low-income economies are defined as those with a
#GNI per capita, calculated using the World Bank Atlas method, of $1,025 or less in 2015;
#lower middle-income economies are those with a GNI per capita between $1,026 and $4,035;
#upper middle-income economies are those with a GNI per capita between $4,036 and $12,475;
#high-income economies are those with a GNI per capita of $12,476 or more.
#https://datahelpdesk.worldbank.org/knowledgebase/articles/906519-world-bank-country-and-lending-groups
# Polity scores range from -10 for undemocratic to +10 for highly democratic countries.
#
# REST OF ASSIGNMENT FOCUSES ON DEMOCRATIC (Polity Score > 0 )
# LOWER MIDDLE-INCOME COUNTRIES ($1025 < GNI < $4036)
########################################################################################
print ('_____________________________________________________')
print('Rest of Assignment will Focus on Highly Democratic Lower Mid-Income Countries')
print('Highly Democratic Countries Have Polity Scores > 0 ')
print('Lower Middle-Income Countries have $1025 < Per Capital Income < $4036 ')
print ('_____________________________________________________')
# Sub-sampling entire data set to only pick up Highly Democratic lower middle-income countries
sub1=data[(data['incomeperperson']>1025) & (data['incomeperperson']<4036) & (data['polityscore']> 0)]
#make a copy of my new subsetted data
sub2 = sub1.copy()
# frequency distritions on new sub2 data frame
# First Fill in Blanks with NaN. Then print out original counts
print ('Original Counts for FemaleEmployRate For Lower Democratic Lower Mid-Income Countries, Sorted by Values')
c5 = sub2['femaleemployrate'].value_counts(sort=True,dropna=False).sort_index(ascending=True)
print(c5)
sub2['femaleemployrate']=sub2['femaleemployrate'].replace(-9.0, numpy.nan)
sub2['femaleemployrate'].fillna(-99.0,inplace=True)
print ('New Counts for FemaleEmployRate For Democratic Lower Mid-Income Countries, Sorted by Values')
c55 = sub2['femaleemployrate'].value_counts(sort=True,dropna=False).sort_index(ascending=True)
print(c55)
#sub2['femaleemployeerate']=sub2['femaleemployrate'].replace(-99.0,numpy.nan)
#c2=sub2['femaleemployrate'].value_counts(sort=True,dropna=False)
#print(c2)
#chk2=sub2['femaleemployrate'].value_counts(sort=True,dropna=True)
#print(chk2)
ds2=sub2['femaleemployrate'].describe()
print('Statistics for Female Employment Rate')
print(ds2)
print('____________________________________________')
print('I will now delete data points for which the female Employment Rate is Missing or NaN' )
sub1=data[(data['incomeperperson']>1025) & (data['incomeperperson']<4036) & (data['polityscore']> 0) & (data['femaleemployrate']>0)]
sub2 = sub1.copy()
recode1={1:10,2:20,3:30,4:40,5:50,6:60,7:70,8:80,9:90,10:100}
print('Introducing Democratic Score, which is mapped from Polity Score')
print('Polity Score Ranges from 1 to 10, and Democratic Score Ranges from 10 to 100')
sub2['democraticscore']=sub2['polityscore'].map(recode1)
print('Introducing New Variable: Governance Score')
print('GOVERNANCE SCORE=Democratic Score+Female Employment Rate+Urban Rate')
sub2['GOVERNANCESCORE']=sub2['democraticscore'] + sub2['femaleemployrate']+sub2['urbanrate']
c3=sub2['GOVERNANCESCORE'].value_counts().sort_index(ascending=True)
print ('GOVERNANCE SCORES')
print(c3)
sub2['INCOMEGROUP']=pandas.qcut(sub2.incomeperperson, 5, labels=["1=0-20","2=20-40","3=40-60","4=60-80","5=80-100"])
c4 = sub2['INCOMEGROUP'].value_counts(sort=False, dropna=True)
print(c4)
print('I break up the Middle-Income group of countries into four sub-groups')
print('The four sub-groups have per capital income: $1025-$1525,$1525-$2025,$2025-$2525,$2525-$3025,$3025-3525,$3525-$4036')
print('INCOME-6 categories: 1025-1525, 1525-2025, 2025-2525, 2525-3025, 3025-3525, 3525-4036')
sub2['INCOMEGROUP']=pandas.cut(sub2.incomeperperson,[1025,1525,2025,2525,3025,3525,4036])
c5=sub2['INCOMEGROUP'].value_counts(sort=True,dropna=True)
print(c5)
print('printing crosstabs')
print(pandas.crosstab(sub2['INCOMEGROUP'],sub2['incomeperperson']))
print('____________________________________________')
print ('Counts for Income Per Person for Democratic Lower Middle-Income Countries, Sorted by Values')
c6 = sub2['incomeperperson'].value_counts().sort_index(ascending=True)
print(c6)
print ('Percentages for Income Per Person for Democratic Lower Middle-Income Countries, Sorted by Values')
p6 = sub2['incomeperperson'].value_counts(normalize=True).sort_index(ascending=True) * 100
print (p6)
print ('Counts for Female Employ Rate for Democratic Lower Middle-Income Countries, Sorted by Values')
c7 = sub2['femaleemployrate'].value_counts().sort_index(ascending=True)
print(c7)
print ('Percentages for Female Employ Rate for Democratic Lower Middle-Income Countries, Sorted by Values')
p7 = sub2['femaleemployrate'].value_counts(normalize=True).sort_index(ascending=True) * 100
print (p7)
print ('Counts for Polity Scores for Democratic Lower Middle-Income Countries, Sorted by Values')
c8 = sub2['polityscore'].value_counts().sort_index(ascending=True)
print (c8)
print ('Percentages for Polity Scores for Democratic Lower Middle-Income Countries, Sorted by Values')
p8 = sub2['polityscore'].value_counts(normalize=True).sort_index(ascending=True) * 100
print (p8)
print('Plotting Income Per Person Univariate Bar Graph')
ax = seaborn.countplot(x="INCOMEGROUP", data=sub2)
plt.xlabel('income per person')
plt.title('INCOME PER PERSON')
print('BASIC STATISTICS ABOUT INCOME PER PERSON GROUP')
ds10 = sub2['INCOMEGROUP'].describe()
print(ds10)
c10 = sub2.groupby('INCOMEGROUP').size()
print(c10)
print('mode of INCOME GROUP')
mode1 = sub2['INCOMEGROUP'].mode()
print(mode1)
print('mean of income per person')
mean1 = sub2['incomeperperson'].mean()
print(mean1)
print('std of income per person')
std1 = sub2['incomeperperson'].std()
print(std1)
print('min of income per person')
min1 = sub2['INCOMEGROUP'].min()
print(min1)
print('max of income per person')
max1 = sub2['INCOMEGROUP'].max()
print(max1)
print('median of income per person')
median1 = sub2['incomeperperson'].median()
print(median1)
c1 = sub2.groupby('INCOMEGROUP').size()
print(c1)
p1 = sub2.groupby('INCOMEGROUP').size() * 100 / len (data)
print(p1)
print('__________________________________________________')
print('LOOKING AT POLITY SCORE')
print('Plotting POLITY SCORE Bar Graph')
#ax = seaborn.countplot(y="democraticscore", data=sub2)
#plt.xlabel('DEMOCRATIC SCORE')
#plt.title('DEMOCRATIC SCORE OF LOWER MIDDLE-INCOME DEMOCRATIC NATIONS')
print('BASIC STATISTICS ABOUT INCOME PER PERSON GROUP')
ds10 = sub2['democraticscore'].describe()
print(ds10)
c10 = sub2.groupby('democraticscore').size()
print(c10)
print('mode of DEMOCRATIC SCORE')
mode1 = sub2['democraticscore'].mode()
print(mode1)
print('mean of DEMOCRATIC SCORE')
mean1 = sub2['democraticscore'].mean()
print(mean1)
print('std of democratic score')
std1 = sub2['democraticscore'].std()
print(std1)
print('min of democraticscore')
min1 = sub2['democraticscore'].min()
print(min1)
print('max of democraticscore')
max1 = sub2['democraticscore'].max()
print(max1)
print('median of democraticscore')
median1 = sub2['democraticscore'].median()
print(median1)
c1 = sub2.groupby('democraticscore').size()
print(c1)
p1 = sub2.groupby('democraticscore').size() * 100 / len (data)
print(p1)
print ('OUTPUT PRINTOUT ENDS HERE')
print ('_____________________________________________________')
#upper-case all DataFrame column names - place afer code for loading data aboave
data.columns = map(str.upper, data.columns)
# bug fix for display formats to avoid run time errors - put after code for loading data above
pandas.set_option('display.float_format', lambda x:'%f'%x)
print('END OF OUTPUT FROM FIRST HALF OF PYTHON CODE FOR ASSIGNMENT 3')
print('_______________________________________________________________')
Now, the output from the first half of the python code:
-----------------------------------------------------------------------------------
runfile('C:/Users/sudhir/Desktop/PERSONAL.BIZ.WORK.JUL2017/COURSERA/DataAnalysisInterPret/WEEK4/SudhirAssignment4.Part1.Part1.1.py', wdir='C:/Users/sudhir/Desktop/PERSONAL.BIZ.WORK.JUL2017/COURSERA/DataAnalysisInterPret/WEEK4')
_____________________________________________________
OUTPUT PRINTOUT STARTS HERE
Total Number of Countries
213
Total Number of Variables
6
The Variables in this Database:
(1) country, (2) incomeperperson, (3) polityscore
(4) femaleemployeerate, (5) internetuserate, and (6) urbanrate
Examples of Sorting by Frequency and by Values
List Polity Scores for Entire Data Set, Sorted by Frequency
10.000000 33
8.000000 19
9.000000 15
7.000000 13
-7.000000 12
6.000000 10
5.000000 7
-4.000000 6
0.000000 6
-3.000000 6
-2.000000 5
-1.000000 4
-9.000000 4
4.000000 4
1.000000 3
-6.000000 3
2.000000 3
-8.000000 2
3.000000 2
-5.000000 2
-10.000000 2
Name: polityscore, dtype: int64
List Polity Scores for Entire Data Set, sorted by Values
polityscore
-10.000000 2
-9.000000 4
-8.000000 2
-7.000000 12
-6.000000 3
-5.000000 2
-4.000000 6
-3.000000 6
-2.000000 5
-1.000000 4
0.000000 6
1.000000 3
2.000000 3
3.000000 2
4.000000 4
5.000000 7
6.000000 10
7.000000 13
8.000000 19
9.000000 15
10.000000 33
dtype: int64
_____________________________________________________
Rest of Assignment will Focus on Highly Democratic Lower Mid-Income Countries
Highly Democratic Countries Have Polity Scores > 0
Lower Middle-Income Countries have $1025 < Per Capital Income < $4036
_____________________________________________________
Original Counts for FemaleEmployRate For Lower Democratic Lower Mid-Income Countries, Sorted by Values
26.799999 1
31.700001 1
34.200001 2
34.299999 1
37.299999 1
39.200001 1
39.900002 1
41.799999 1
42.099998 2
43.799999 1
44.000000 1
44.099998 1
44.799999 1
45.500000 1
46.799999 1
47.500000 1
48.500000 1
49.400002 1
51.299999 1
53.799999 1
54.900002 1
59.799999 1
61.599998 1
65.000000 1
65.300003 1
nan 2
Name: femaleemployrate, dtype: int64
New Counts for FemaleEmployRate For Democratic Lower Mid-Income Countries, Sorted by Values
-99.000000 2
26.799999 1
31.700001 1
34.200001 2
34.299999 1
37.299999 1
39.200001 1
39.900002 1
41.799999 1
42.099998 2
43.799999 1
44.000000 1
44.099998 1
44.799999 1
45.500000 1
46.799999 1
47.500000 1
48.500000 1
49.400002 1
51.299999 1
53.799999 1
54.900002 1
59.799999 1
61.599998 1
65.000000 1
65.300003 1
Name: femaleemployrate, dtype: int64
Statistics for Female Employment Rate
count 29.000000
mean 35.575862
std 38.482256
min -99.000000
25% 37.299999
50% 44.000000
75% 49.400002
max 65.300003
Name: femaleemployrate, dtype: float64
____________________________________________
I will now delete data points for which the female Employment Rate is Missing or NaN
Introducing Democratic Score, which is mapped from Polity Score
Polity Score Ranges from 1 to 10, and Democratic Score Ranges from 10 to 100
Introducing New Variable: Governance Score
GOVERNANCE SCORE=Democratic Score+Female Employment Rate+Urban Rate
GOVERNANCE SCORES
104.380002 1
114.300001 1
116.920001 1
130.179999 1
134.139999 1
138.320000 1
148.060001 1
149.259999 1
152.080001 1
161.080000 1
161.240000 1
166.639999 1
172.580000 1
175.559998 1
178.819998 1
183.699999 1
185.039999 1
185.499999 1
187.380002 1
188.039999 1
190.800000 1
191.719999 1
197.179998 1
199.400002 1
203.199998 1
205.600003 1
221.199999 1
Name: GOVERNANCESCORE, dtype: int64
1=0-20 6
2=20-40 5
3=40-60 5
4=60-80 5
5=80-100 6
Name: INCOMEGROUP, dtype: int64
I break up the Middle-Income group of countries into four sub-groups
The four sub-groups have per capital income: $1025-$1525,$1525-$2025,$2025-$2525,$2525-$3025,$3025-3525,$3525-$4036
INCOME-6 categories: 1025-1525, 1525-2025, 2025-2525, 2525-3025, 3025-3525, 3525-4036
(1025, 1525] 11
(2525, 3025] 6
(1525, 2025] 4
(3525, 4036] 2
(3025, 3525] 2
(2025, 2525] 2
Name: INCOMEGROUP, dtype: int64
printing crosstabs
incomeperperson 1036.830725 1143.831514 1144.102193 1200.652075 \
INCOMEGROUP
(1025, 1525] 1 1 1 1
(1525, 2025] 0 0 0 0
(2025, 2525] 0 0 0 0
(2525, 3025] 0 0 0 0
(3025, 3525] 0 0 0 0
(3525, 4036] 0 0 0 0
incomeperperson 1232.794137 1258.762596 1295.742686 1324.194906 \
INCOMEGROUP
(1025, 1525] 1 1 1 1
(1525, 2025] 0 0 0 0
(2025, 2525] 0 0 0 0
(2525, 3025] 0 0 0 0
(3025, 3525] 0 0 0 0
(3525, 4036] 0 0 0 0
incomeperperson 1326.741757 1383.401869 ... 2549.558474 \
INCOMEGROUP ...
(1025, 1525] 1 1 ... 0
(1525, 2025] 0 0 ... 0
(2025, 2525] 0 0 ... 0
(2525, 3025] 0 0 ... 1
(3025, 3525] 0 0 ... 0
(3525, 4036] 0 0 ... 0
incomeperperson 2557.433638 2636.787800 2667.246710 2712.517199 \
INCOMEGROUP
(1025, 1525] 0 0 0 0
(1525, 2025] 0 0 0 0
(2025, 2525] 0 0 0 0
(2525, 3025] 1 1 1 1
(3025, 3525] 0 0 0 0
(3525, 4036] 0 0 0 0
incomeperperson 2923.144355 3180.430612 3233.423780 3665.348369 \
INCOMEGROUP
(1025, 1525] 0 0 0 0
(1525, 2025] 0 0 0 0
(2025, 2525] 0 0 0 0
(2525, 3025] 1 0 0 0
(3025, 3525] 0 1 1 0
(3525, 4036] 0 0 0 1
incomeperperson 3745.649852
INCOMEGROUP
(1025, 1525] 0
(1525, 2025] 0
(2025, 2525] 0
(2525, 3025] 0
(3025, 3525] 0
(3525, 4036] 1
____________________________________________
Counts for Income Per Person for Democratic Lower Middle-Income Countries, Sorted by Values
1036.830725 1
1143.831514 1
1144.102193 1
1200.652075 1
1232.794137 1
1258.762596 1
1295.742686 1
1324.194906 1
1326.741757 1
1383.401869 1
1392.411829 1
1621.177078 1
1728.020976 1
1860.753895 1
1914.996551 1
2221.185664 1
2231.993335 1
2549.558474 1
2557.433638 1
2636.787800 1
2667.246710 1
2712.517199 1
2923.144355 1
3180.430612 1
3233.423780 1
3665.348369 1
3745.649852 1
Name: incomeperperson, dtype: int64
Percentages for Income Per Person for Democratic Lower Middle-Income Countries, Sorted by Values
1036.830725 3.703704
1143.831514 3.703704
1144.102193 3.703704
1200.652075 3.703704
1232.794137 3.703704
1258.762596 3.703704
1295.742686 3.703704
1324.194906 3.703704
1326.741757 3.703704
1383.401869 3.703704
1392.411829 3.703704
1621.177078 3.703704
1728.020976 3.703704
1860.753895 3.703704
1914.996551 3.703704
2221.185664 3.703704
2231.993335 3.703704
2549.558474 3.703704
2557.433638 3.703704
2636.787800 3.703704
2667.246710 3.703704
2712.517199 3.703704
2923.144355 3.703704
3180.430612 3.703704
3233.423780 3.703704
3665.348369 3.703704
3745.649852 3.703704
Name: incomeperperson, dtype: float64
Counts for Female Employ Rate for Democratic Lower Middle-Income Countries, Sorted by Values
26.799999 1
31.700001 1
34.200001 2
34.299999 1
37.299999 1
39.200001 1
39.900002 1
41.799999 1
42.099998 2
43.799999 1
44.000000 1
44.099998 1
44.799999 1
45.500000 1
46.799999 1
47.500000 1
48.500000 1
49.400002 1
51.299999 1
53.799999 1
54.900002 1
59.799999 1
61.599998 1
65.000000 1
65.300003 1
Name: femaleemployrate, dtype: int64
Percentages for Female Employ Rate for Democratic Lower Middle-Income Countries, Sorted by Values
26.799999 3.703704
31.700001 3.703704
34.200001 7.407407
34.299999 3.703704
37.299999 3.703704
39.200001 3.703704
39.900002 3.703704
41.799999 3.703704
42.099998 7.407407
43.799999 3.703704
44.000000 3.703704
44.099998 3.703704
44.799999 3.703704
45.500000 3.703704
46.799999 3.703704
47.500000 3.703704
48.500000 3.703704
49.400002 3.703704
51.299999 3.703704
53.799999 3.703704
54.900002 3.703704
59.799999 3.703704
61.599998 3.703704
65.000000 3.703704
65.300003 3.703704
Name: femaleemployrate, dtype: float64
Counts for Polity Scores for Democratic Lower Middle-Income Countries, Sorted by Values
2.000000 1
3.000000 1
4.000000 2
5.000000 2
6.000000 4
7.000000 4
8.000000 6
9.000000 7
Name: polityscore, dtype: int64
Percentages for Polity Scores for Democratic Lower Middle-Income Countries, Sorted by Values
2.000000 3.703704
3.000000 3.703704
4.000000 7.407407
5.000000 7.407407
6.000000 14.814815
7.000000 14.814815
8.000000 22.222222
9.000000 25.925926
Name: polityscore, dtype: float64
Plotting Income Per Person Univariate Bar Graph
BASIC STATISTICS ABOUT INCOME PER PERSON GROUP
count 27
unique 6
top (1025, 1525]
freq 11
Name: INCOMEGROUP, dtype: object
INCOMEGROUP
(1025, 1525] 11
(1525, 2025] 4
(2025, 2525] 2
(2525, 3025] 6
(3025, 3525] 2
(3525, 4036] 2
dtype: int64
mode of INCOME GROUP
0 (1025, 1525]
Name: INCOMEGROUP, dtype: category
Categories (6, interval[int64]): [(1025, 1525] < (1525, 2025] < (2025, 2525] < (2525, 3025] < (3025, 3525] < (3525, 4036]]
mean of income per person
2044.0420212962965
std of income per person
836.1734117910694
min of income per person
(1025, 1525]
max of income per person
(3525, 4036]
median of income per person
1860.7538949999998
INCOMEGROUP
(1025, 1525] 11
(1525, 2025] 4
(2025, 2525] 2
(2525, 3025] 6
(3025, 3525] 2
(3525, 4036] 2
dtype: int64
INCOMEGROUP
(1025, 1525] 5.164319
(1525, 2025] 1.877934
(2025, 2525] 0.938967
(2525, 3025] 2.816901
(3025, 3525] 0.938967
(3525, 4036] 0.938967
dtype: float64
__________________________________________________
LOOKING AT POLITY SCORE
Plotting POLITY SCORE Bar Graph
BASIC STATISTICS ABOUT INCOME PER PERSON GROUP
count 27.000000
mean 68.888889
std 20.064000
min 20.000000
25% 60.000000
50% 70.000000
75% 85.000000
max 90.000000
Name: democraticscore, dtype: float64
democraticscore
20 1
30 1
40 2
50 2
60 4
70 4
80 6
90 7
dtype: int64
mode of DEMOCRATIC SCORE
0 90
dtype: int64
mean of DEMOCRATIC SCORE
68.88888888888889
std of democratic score
20.06400016357911
min of democraticscore
20
max of democraticscore
90
median of democraticscore
70.0
democraticscore
20 1
30 1
40 2
50 2
60 4
70 4
80 6
90 7
dtype: int64
democraticscore
20 0.469484
30 0.469484
40 0.938967
50 0.938967
60 1.877934
70 1.877934
80 2.816901
90 3.286385
dtype: float64
The Second Half of the python Code is below:
----------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 17 23:50:31 2017
import pandas
import numpy
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv('gapminder.csv', low_memory=False)
#making individual PROGRESS variables numeric
data['incomeperperson'] = data['incomeperperson'].convert_objects(convert_numeric=True)
data['employrate'] = data['employrate'].convert_objects(convert_numeric=True)
data['femaleemployrate'] = data['femaleemployrate'].convert_objects(convert_numeric=True)
data['internetuserate'] = data['internetuserate'].convert_objects(convert_numeric=True)
data['urbanrate'] = data['urbanrate'].convert_objects(convert_numeric=True)
data['armedforcesrate'] = data['armedforcesrate'].convert_objects(convert_numeric=True)
data['hivrate'] = data['hivrate'].convert_objects(convert_numeric=True)
#Set missing data to NAN
data['employrate']=data['employrate'].replace('', numpy.nan)
data['femaleemployrate']=data['femaleemployrate'].replace('', numpy.nan)
data['internetuserate']=data['internetuserate'].replace('', numpy.nan)
data['urbanrate']=data['urbanrate'].replace('', numpy.nan)
data['armedforcesrate']=data['armedforcesrate'].replace('', numpy.nan)
data['incomeperperson']=data['incomeperperson'].replace('', numpy.nan)
#count of number of PROGRESS categories endorsed, GENDEREQUITY
data['GENDEREQUITY']=data['femaleemployrate'] - data['employrate']
print('GENDEREQUITY')
# subset variables in new data frame, sub1
sub1=data[['country','employrate', 'femaleemployrate', 'internetuserate', 'urbanrate', 'armedforcesrate', 'GENDEREQUITY']]
sub1=data[(data['femaleemployrate']>=40.0) & (data['internetuserate']>60)]
print ('printing a before PROGRESS variable,n=5')
a = sub1.head (n=10)
print(a)
#new PROGRESS variable, categorical 1 through 6
def PROGRESS (row):
if (row['GENDEREQUITY'] > 0) & (row['femaleemployrate'] > 50) :
# if GENDEREQUITY > 0, then female employment rate is greater
# than male employment rate, indicating great gender equity
return 1
# if gender equity < 0, we look at employment rate. If that is
# greater than 50%, then the country has high employment
if (row['GENDEREQUITY'] > 0) & (row['femaleemployrate'] <= 50) :
# if GENDEREQUITY > 0, then female employment rate is greater
# than male employment rate, indicating great gender equity
return 2
# if gender equity < 0, we look at employment rate. If that is
# greater than 50%, then the country has high employment
if (row['GENDEREQUITY'] < 0) & (row['GENDEREQUITY'] >= -10) & (row['femaleemployrate'] >= 50) :
return 3
if (row['GENDEREQUITY'] < 0) & (row['GENDEREQUITY'] >= -10) & (row['femaleemployrate'] <= 50):
return 4
if (row['GENDEREQUITY'] < -10) & (row['femaleemployrate'] >= 50):
return 5
if (row['GENDEREQUITY'] < -10) & (row['femaleemployrate'] < 50):
return 6
sub1['PROGRESS'] = sub1.apply (lambda row: PROGRESS (row),axis=1)
print ('printing a after PROGRESS variable,n=7')
a = sub1.head (n=12)
print(a)
#frequency distributions for primary and secondary ethinciity variables
print ('counts for Employ Rate')
c10 = sub1['employrate'].value_counts(sort=True)
print(c10)
print ('percentages for Employ Rate')
p10 = sub1['employrate'].value_counts(sort=True, normalize=True)
print (p10)
print ('counts for Female Employ Rate')
c11 = sub1['femaleemployrate'].value_counts(sort=True)
print(c11)
print ('percentages for Female Employ Rate')
p11= sub1['femaleemployrate'].value_counts(sort=True, normalize=True)
print (p11)
print ('counts for Internet Use Rate')
c12 = sub1['internetuserate'].value_counts(sort=True)
print(c12)
print ('percentages for Internet Use Rate')
p12 = sub1['internetuserate'].value_counts(sort=True, normalize=True)
print (p12)
print ('counts for Urban Rate')
c13 = sub1['urbanrate'].value_counts(sort=True)
#print(c13)
print ('percentages for Urban Rate')
p13 = sub1['urbanrate'].value_counts(sort=True, normalize=True)
#print (p13)
print ('counts for Armed Forces Rate')
c14 = sub1['armedforcesrate'].value_counts(sort=True)
#print(c14)
print ('percentages for Armed Forces Rate')
p14 = sub1['armedforcesrate'].value_counts(sort=True, normalize=True)
#print (p14)
print ('counts for number of Gender Equity')
c15 = sub1['GENDEREQUITY'].value_counts(sort=True)
print(c15)
print ('counts for PROGRESS CATEGORIES')
c16 = sub1['PROGRESS'].value_counts(sort=True)
print(c16)
print ('percentages for PROGRESS CATEGORIES')
p16 = sub1['PROGRESS'].value_counts(sort=True, normalize=True)
print (p16)
#basic scatterplot: Q->Q
scat1 = seaborn.regplot(x="urbanrate", y="internetuserate", fit_reg=False, data=data)
plt.xlabel('Urban Rate')
plt.ylabel('Internet Use Rate')
plt.title('Scatterplot for the Association Between Urban Rate and Internet Use Rate')
scat2 = seaborn.regplot(x="urbanrate", y="internetuserate", data=data)
plt.xlabel('Urban Rate')
plt.ylabel('Internet Use Rate')
plt.title('Scatterplot for the Association Between Urban Rate and Internet Use Rate')
scat3 = seaborn.regplot(x="incomeperperson", y="internetuserate", data=data)
plt.xlabel('Income per Person')
plt.ylabel('Internet Use Rate')
plt.title('Scatterplot for the Association Between Income per Person and Internet Use Rate')
scat4 = seaborn.regplot(x="incomeperperson", y="hivrate", data=data)
plt.xlabel('Income per Person')
plt.ylabel('HIV Rate')
plt.title('Scatterplot for the Association Between Income per Person and HIV Rate')
# quartile split (use qcut function & ask for 4 groups - gives you quartile split)
print ('Income per person - 4 categories - quartiles')
data['INCOMEGRP4']=pandas.qcut(data.incomeperperson, 4, labels=["1=25th%tile","2=50%tile","3=75%tile","4=100%tile"])
c10 = data['INCOMEGRP4'].value_counts(sort=False, dropna=True)
print(c10)
# bivariate bar graph C->Q
seaborn.factorplot(x='INCOMEGRP4', y='hivrate', data=data, kind="bar", ci=None)
plt.xlabel('income group')
plt.ylabel('mean HIV rate')
print('PRINTING INCOMEGRP4 NOW')
c11= data.groupby('INCOMEGRP4').size()
print (c11)
print('-------------------------------------------------')
#result = data.sort(['INCOMEGRP4'], ascending=[1])
#print(result)
-----------------------------------------------------------------------------------
The Output from the second part of the python code is below:
-----------------------------------------------------------------------------------
printing a before PROGRESS variable,n=5
country incomeperperson alcconsumption armedforcesrate \
9 Australia 25249.986060 10.21 0.486280
10 Austria 26692.984110 12.4 0.815580
15 Barbados 9243.587053 6.42 0.663956
17 Belgium 24496.048260 10.41 0.815648
32 Canada 25575.352620 10.2 0.342976
49 Czech Rep. 7381.312751 16.47 0.515706
50 Denmark 30532.277040 12.02 1.012373
59 Estonia 6238.537506 17.24 0.998428
63 Finland 27110.731590 13.1 1.177416
64 France 22878.466570 12.48 1.233780
breastcancerper100th co2emissions femaleemployrate hivrate \
9 83.2 12970092667 54.599998 0.100000
10 70.5 4466084333 49.700001 0.300000
15 62.5 36160666.67 60.299999 1.400000
17 92 10897025333 41.700001 0.200000
32 84.3 24979045667 58.900002 0.200000
49 58.4 1776016000 47.599998 0.060000
50 88.7 3503877667 58.099998 0.200000
59 47.7 277170666.7 52.099998 1.200000
63 84.7 2420300667 53.400002 0.100000
64 91.9 33341634333 45.599998 0.400000
internetuserate lifeexpectancy oilperperson polityscore \
9 75.895654 81.907 1.913026109 10
10 72.731576 80.854 1.548790966 10
15 70.028599 76.835
17 73.733934 80.009 8
32 81.338393 81.012 3.007355851 10
49 68.638133 77.685 0.876778335 8
50 88.770254 78.826 1.567527461 10
59 74.163040 74.825 9
63 86.898845 79.977 1.938654268 10
64 77.498619 81.539 1.328291411 9
relectricperperson suicideper100th employrate urbanrate GENDEREQUITY
9 2825.391095 8.470030125 61.500000 88.740000 -6.900002
10 2068.123309 13.09437 57.099998 67.160000 -7.399998
15 3.108602524 66.900002 39.840000 -6.600002
17 1920.962215 15.95385 48.599998 97.360000 -6.899998
32 4772.370648 10.10099 63.500000 80.400000 -4.599998
49 1438.780412 12.36798 56.000000 73.500000 -8.400002
50 1884.299342 8.973104 63.099998 86.680000 -5.000000
59 1411.230532 16.95924 56.500000 69.460000 -4.400002
63 4036.953993 16.23437 57.200001 63.300000 -3.799999
64 2539.753273 14.09153 51.200001 77.360000 -5.600002
printing a after PROGRESS variable,n=7
country incomeperperson alcconsumption armedforcesrate \
9 Australia 25249.986060 10.21 0.486280
10 Austria 26692.984110 12.4 0.815580
15 Barbados 9243.587053 6.42 0.663956
17 Belgium 24496.048260 10.41 0.815648
32 Canada 25575.352620 10.2 0.342976
49 Czech Rep. 7381.312751 16.47 0.515706
50 Denmark 30532.277040 12.02 1.012373
59 Estonia 6238.537506 17.24 0.998428
63 Finland 27110.731590 13.1 1.177416
64 France 22878.466570 12.48 1.233780
69 Germany 25306.187190 12.14 0.575810
83 Hong Kong, China 35536.072470 nan
breastcancerper100th co2emissions femaleemployrate hivrate \
9 83.2 12970092667 54.599998 0.100000
10 70.5 4466084333 49.700001 0.300000
15 62.5 36160666.67 60.299999 1.400000
17 92 10897025333 41.700001 0.200000
32 84.3 24979045667 58.900002 0.200000
49 58.4 1776016000 47.599998 0.060000
50 88.7 3503877667 58.099998 0.200000
59 47.7 277170666.7 52.099998 1.200000
63 84.7 2420300667 53.400002 0.100000
64 91.9 33341634333 45.599998 0.400000
69 79.8 41229554667 46.799999 0.100000
83 1026813333 51.599998 nan
internetuserate lifeexpectancy oilperperson polityscore \
9 75.895654 81.907 1.913026109 10
10 72.731576 80.854 1.548790966 10
15 70.028599 76.835
17 73.733934 80.009 8
32 81.338393 81.012 3.007355851 10
49 68.638133 77.685 0.876778335 8
50 88.770254 78.826 1.567527461 10
59 74.163040 74.825 9
63 86.898845 79.977 1.938654268 10
64 77.498619 81.539 1.328291411 9
69 82.526898 80.414 1.398500033 10
83 71.849124 82.759 2.282655406
relectricperperson suicideper100th employrate urbanrate GENDEREQUITY \
9 2825.391095 8.470030125 61.500000 88.740000 -6.900002
10 2068.123309 13.09437 57.099998 67.160000 -7.399998
15 3.108602524 66.900002 39.840000 -6.600002
17 1920.962215 15.95385 48.599998 97.360000 -6.899998
32 4772.370648 10.10099 63.500000 80.400000 -4.599998
49 1438.780412 12.36798 56.000000 73.500000 -8.400002
50 1884.299342 8.973104 63.099998 86.680000 -5.000000
59 1411.230532 16.95924 56.500000 69.460000 -4.400002
63 4036.953993 16.23437 57.200001 63.300000 -3.799999
64 2539.753273 14.09153 51.200001 77.360000 -5.600002
69 1693.891898 9.211085 53.500000 73.640000 -6.700001
83 1468.640784 59.000000 100.000000 -7.400002
PROGRESS
9 3
10 4
15 3
17 4
32 3
49 4
50 3
59 3
63 3
64 4
69 4
83 3
counts for Employ Rate
53.500000 2
65.000000 2
48.599998 1
62.299999 1
66.900002 1
63.500000 1
56.000000 1
56.500000 1
59.000000 1
73.599998 1
47.299999 1
57.200001 1
59.900002 1
52.500000 1
57.099998 1
53.099998 1
60.700001 1
58.900002 1
51.200001 1
62.400002 1
61.299999 1
51.299999 1
53.400002 1
48.700001 1
56.799999 1
55.900002 1
63.099998 1
64.300003 1
59.299999 1
57.299999 1
61.500000 1
Name: employrate, dtype: int64
percentages for Employ Rate
53.500000 0.060606
65.000000 0.060606
48.599998 0.030303
62.299999 0.030303
66.900002 0.030303
63.500000 0.030303
56.000000 0.030303
56.500000 0.030303
59.000000 0.030303
73.599998 0.030303
47.299999 0.030303
57.200001 0.030303
59.900002 0.030303
52.500000 0.030303
57.099998 0.030303
53.099998 0.030303
60.700001 0.030303
58.900002 0.030303
51.200001 0.030303
62.400002 0.030303
61.299999 0.030303
51.299999 0.030303
53.400002 0.030303
48.700001 0.030303
56.799999 0.030303
55.900002 0.030303
63.099998 0.030303
64.300003 0.030303
59.299999 0.030303
57.299999 0.030303
61.500000 0.030303
Name: employrate, dtype: float64
counts for Female Employ Rate
41.700001 2
47.599998 1
45.599998 1
58.900002 1
48.000000 1
51.599998 1
57.000000 1
54.299999 1
56.000000 1
69.599998 1
60.299999 1
49.700001 1
53.400002 1
56.700001 1
54.599998 1
45.299999 1
40.299999 1
50.700001 1
53.099998 1
49.400002 1
46.400002 1
58.299999 1
52.099998 1
45.900002 1
46.200001 1
48.799999 1
60.900002 1
58.099998 1
42.099998 1
51.299999 1
46.799999 1
51.000000 1
Name: femaleemployrate, dtype: int64
percentages for Female Employ Rate
41.700001 0.060606
47.599998 0.030303
45.599998 0.030303
58.900002 0.030303
48.000000 0.030303
51.599998 0.030303
57.000000 0.030303
54.299999 0.030303
56.000000 0.030303
69.599998 0.030303
60.299999 0.030303
49.700001 0.030303
53.400002 0.030303
56.700001 0.030303
54.599998 0.030303
45.299999 0.030303
40.299999 0.030303
50.700001 0.030303
53.099998 0.030303
49.400002 0.030303
46.400002 0.030303
58.299999 0.030303
52.099998 0.030303
45.900002 0.030303
46.200001 0.030303
48.799999 0.030303
60.900002 0.030303
58.099998 0.030303
42.099998 0.030303
51.299999 0.030303
46.799999 0.030303
51.000000 0.030303
Name: femaleemployrate, dtype: float64
counts for Internet Use Rate
90.016190 1
93.277508 1
62.811900 1
72.731576 1
74.247572 1
82.515928 1
81.338393 1
69.770394 1
70.028599 1
71.849124 1
83.002584 1
75.895654 1
65.387786 1
73.733934 1
84.731705 1
79.889777 1
82.526898 1
90.079527 1
82.166660 1
62.471230 1
65.808554 1
68.638133 1
86.898845 1
71.131707 1
65.163251 1
74.163040 1
77.498619 1
95.638113 1
69.339971 1
71.514724 1
88.770254 1
90.703555 1
77.638535 1
Name: internetuserate, dtype: int64
percentages for Internet Use Rate
90.016190 0.030303
93.277508 0.030303
62.811900 0.030303
72.731576 0.030303
74.247572 0.030303
82.515928 0.030303
81.338393 0.030303
69.770394 0.030303
70.028599 0.030303
71.849124 0.030303
83.002584 0.030303
75.895654 0.030303
65.387786 0.030303
73.733934 0.030303
84.731705 0.030303
79.889777 0.030303
82.526898 0.030303
90.079527 0.030303
82.166660 0.030303
62.471230 0.030303
65.808554 0.030303
68.638133 0.030303
86.898845 0.030303
71.131707 0.030303
65.163251 0.030303
74.163040 0.030303
77.498619 0.030303
95.638113 0.030303
69.339971 0.030303
71.514724 0.030303
88.770254 0.030303
90.703555 0.030303
77.638535 0.030303
Name: internetuserate, dtype: float64
counts for Urban Rate
percentages for Urban Rate
counts for Armed Forces Rate
percentages for Armed Forces Rate
counts for number of Gender Equity
-7.000000 2
-4.000000 2
-6.700001 2
-6.600002 2
-5.600002 1
-7.399998 1
-11.099998 1
-6.500000 1
-6.299999 1
-8.400002 1
-7.300003 1
-4.899998 1
-6.099998 1
-6.900002 1
-7.599998 1
-8.100002 1
-8.900002 1
-6.200001 1
-7.400002 1
-10.799999 1
-3.799999 1
-10.900002 1
-11.100002 1
-6.899998 1
-4.099998 1
-4.299999 1
-4.599998 1
-4.400002 1
-5.000000 1
Name: GENDEREQUITY, dtype: int64
counts for PROGRESS CATEGORIES
3 17
4 12
6 3
5 1
Name: PROGRESS, dtype: int64
percentages for PROGRESS CATEGORIES
3 0.515152
4 0.363636
6 0.090909
5 0.030303
Name: PROGRESS, dtype: float64
Income per person - 4 categories - quartiles
1=25th%tile 48
2=50%tile 47
3=75%tile 47
4=100%tile 48
Name: INCOMEGRP4, dtype: int64
PRINTING INCOMEGRP4 NOW
INCOMEGRP4
1=25th%tile 48
2=50%tile 47
3=75%tile 47
4=100%tile 48
dtype: int64
------------------------------------------------------------------------------------
Summary: There is a lot of information in the output. Just to capture the main points: (a) lower middle-income democratic countries are skewed towards high democratic scores [mode of 90, mean of 68.9 and standard deviation of 20] ; (b) income per person [also referred to as per capita income] seems to correlate with Internet use [i.e, more internet use is linked to higher per capita incomes] in both the scatter plot and histogram plot; (c) higher Income per person is correlated with lower HIV rate as clearly seen in the histogram, but the scatter plot for income per person versus HIV rate seems to imply the opposite [i.e, lower HIV rate is correlated with lower per capita income, but this result is suspect and likely due to variability in HIV rate in middle-income countries]; and (d) Income per person and urban rate is very weakly correlated for lower incomes, with tremendous variability in urban rates for the same Income group [see scatter plot], but there is a clear correlation between income per person and higher urban rates for middle to high income countries, as indicated in the histogram plot.
------------------------------END OF ASSIGNMENT 4--------------------------