Data Analysis and a bit on Democracy pt. 2

By Samuel Muiruri | Dec. 20, 2018 | Python Scripts


Picking up from where I left on the last article here’s the aggregate from Positive affect “not exactly sure what it stands for”, the mean looks to be constant, the min varies but generally in a predictable manner and the max looks to be on the increase. The mean being also that close to the max is also good thing.

df9.groupby('year')['Positive affect'].agg(['mean','max','min']).plot(kind='bar')

and the negative affect with a bigger difference between the minimum and maximum and the mean being far more closer to the minimum than the maximum

df9.groupby('year')['Negative affect'].agg(['mean','max','min']).plot(kind='bar')

Then there’s confidence in the national government, the graph points very similar to the negative effect

df9.groupby('year')['Confidence in national government'].agg(['mean','max','min']).plot(kind='bar')

Going now to the country level starting with Kenya which to be put in context since Uhuru got in power in 2013 there’s been a steady confidence in the government.

df9[df9['country'] == 'Kenya'].groupby('year')['Confidence in national government'].agg(['max']).plot(kind='bar')

US on the other hand seems to have a decreasing confidence in the government

df9[df9['country'] == 'United States'].groupby('year')['Confidence in national government'].agg(['max']).plot(kind='bar')

Uganda on the other hand with Museveni’s rule being considered by some as a totalitarian rule still has a high confidence index and seems to be on the steady increase.

df9[df9['country'] == 'Uganda'].groupby('year')['Confidence in national government'].agg(['max']).plot(kind='bar')

Tanzania without a more democratic rule “I guess” has a higher confidence index

df9[df9['country'] == 'Tanzania'].groupby('year')['Confidence in national government'].agg(['max']).plot(kind='bar')

The next column is Democratic Quality with an interesting positive and negative index

df9.groupby('year')['Democratic Quality'].agg(['mean','max','min']).plot(kind='bar')

First is the list of Democratic countries

df9[df9['Democratic Quality'] < 0]['country'].unique()
>>>
array(['Afghanistan', 'Albania', 'Algeria', 'Angola', 'Armenia',
       'Azerbaijan', 'Bahrain', 'Bangladesh', 'Belarus', 'Bolivia',
       'Bosnia and Herzegovina', 'Burkina Faso', 'Burundi', 'Cambodia',
       'Cameroon', 'Central African Republic', 'Chad', 'China',
       'Colombia', 'Comoros', 'Congo (Brazzaville)', 'Congo (Kinshasa)',
       'Cuba', 'Djibouti', 'Ecuador', 'Egypt', 'El Salvador', 'Ethiopia',
       'Gabon', 'Georgia', 'Guatemala', 'Guinea', 'Guyana', 'Haiti',
       'Honduras', 'India', 'Indonesia', 'Iran', 'Iraq', 'Israel',
       'Ivory Coast', 'Jordan', 'Kazakhstan', 'Kenya', 'Kuwait',
       'Kyrgyzstan', 'Laos', 'Lebanon', 'Lesotho', 'Liberia', 'Libya',
       'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Mali',
       'Mauritania', 'Mexico', 'Moldova', 'Morocco', 'Mozambique',
       'Myanmar', 'Nepal', 'Nicaragua', 'Niger', 'Nigeria', 'Oman',
       'Pakistan', 'Paraguay', 'Peru', 'Philippines', 'Qatar', 'Russia',
       'Rwanda', 'Saudi Arabia', 'Senegal', 'Serbia', 'Sierra Leone',
       'Somalia', 'South Sudan', 'Sri Lanka', 'Sudan', 'Swaziland',
       'Syria', 'Tajikistan', 'Tanzania', 'Thailand', 'Togo', 'Tunisia',
       'Turkey', 'Turkmenistan', 'Uganda', 'Ukraine',
       'United Arab Emirates', 'Uzbekistan', 'Venezuela', 'Vietnam',
       'Yemen', 'Zambia', 'Zimbabwe'], dtype=object)

And un democratic countries at any point

df9[df9['Democratic Quality'] > 0]['country'].unique()
>>>
df9[df9['Democratic Quality'] > 0]['country'].unique()
array(['Albania', 'Argentina', 'Australia', 'Austria', 'Belgium',
       'Belize', 'Benin', 'Bhutan', 'Botswana', 'Brazil', 'Bulgaria',
       'Burkina Faso', 'Canada', 'Chile', 'Costa Rica', 'Croatia',
       'Cyprus', 'Czech Republic', 'Denmark', 'Dominican Republic',
       'El Salvador', 'Estonia', 'Finland', 'France', 'Germany', 'Ghana',
       'Greece', 'Hong Kong S.A.R. of China', 'Hungary', 'Iceland',
       'Ireland', 'Italy', 'Jamaica', 'Japan', 'Latvia', 'Lesotho',
       'Lithuania', 'Luxembourg', 'Macedonia', 'Madagascar', 'Malawi',
       'Mali', 'Malta', 'Mauritius', 'Moldova', 'Mongolia', 'Montenegro',
       'Mozambique', 'Namibia', 'Netherlands', 'New Zealand', 'Norway',
       'Panama', 'Paraguay', 'Peru', 'Poland', 'Portugal', 'Qatar',
       'Romania', 'Senegal', 'Serbia', 'Singapore', 'Slovakia',
       'Slovenia', 'South Africa', 'South Korea', 'Spain', 'Suriname',
       'Sweden', 'Switzerland', 'Taiwan Province of China',
       'Trinidad and Tobago', 'Ukraine', 'United Arab Emirates',
       'United Kingdom', 'United States', 'Uruguay', 'Zambia'],
      dtype=object)

And the list that has never been democratic always had a negative index

undemocratic_countries = set(df9[df9['Democratic Quality'] < 0]['country'].values.tolist())
democratic_countries = set(df9[df9['Democratic Quality'] > 0]['country'].values.tolist())
#those that never been democratic
undemocratic_countries - democratic_countries
>>>
{'Afghanistan',
 'Algeria',
 'Angola',
 'Armenia',
 'Azerbaijan',
 'Bahrain',
 'Bangladesh',
 'Belarus',
 'Bolivia',
 'Bosnia and Herzegovina',
 'Burundi',
 'Cambodia',
 'Cameroon',
 'Central African Republic',
 'Chad',
 'China',
 'Colombia',
 'Comoros',
 'Congo (Brazzaville)',
 'Congo (Kinshasa)',
 'Cuba',
 'Djibouti',
 'Ecuador',
 'Egypt',
 'Ethiopia',
 'Gabon',
 'Georgia',
 'Guatemala',
 'Guinea',
 'Guyana',
 'Haiti',
 'Honduras',
 'India',
 'Indonesia',
 'Iran',
 'Iraq',
 'Israel',
 'Ivory Coast',
 'Jordan',
 'Kazakhstan',
 'Kenya',
 'Kuwait',
 'Kyrgyzstan',
 'Laos',
 'Lebanon',
 'Liberia',
 'Libya',
 'Malaysia',
 'Mauritania',
 'Mexico',
 'Morocco',
 'Myanmar',
 'Nepal',
 'Nicaragua',
 'Niger',
 'Nigeria',
 'Oman',
 'Pakistan',
 'Philippines',
 'Russia',
 'Rwanda',
 'Saudi Arabia',
 'Sierra Leone',
 'Somalia',
 'South Sudan',
 'Sri Lanka',
 'Sudan',
 'Swaziland',
 'Syria',
 'Tajikistan',
 'Tanzania',
 'Thailand',
 'Togo',
 'Tunisia',
 'Turkey',
 'Turkmenistan',
 'Uganda',
 'Uzbekistan',
 'Venezuela',
 'Vietnam',
 'Yemen',
 'Zimbabwe'}

You might raise an eye brow if you note your country or another country you thought was democratic, don’t worry I’m getting to that.

Then those that have always had a positive democratic index

#those that have always been democratic
democratic_countries - undemocratic_countries
>>>
{'Argentina',
 'Australia',
 'Austria',
 'Belgium',
 'Belize',
 'Benin',
 'Bhutan',
 'Botswana',
 'Brazil',
 'Bulgaria',
 'Canada',
 'Chile',
 'Costa Rica',
 'Croatia',
 'Cyprus',
 'Czech Republic',
 'Denmark',
 'Dominican Republic',
 'Estonia',
 'Finland',
 'France',
 'Germany',
 'Ghana',
 'Greece',
 'Hong Kong S.A.R. of China',
 'Hungary',
 'Iceland',
 'Ireland',
 'Italy',
 'Jamaica',
 'Japan',
 'Latvia',
 'Lithuania',
 'Luxembourg',
 'Malta',
 'Mauritius',
 'Mongolia',
 'Montenegro',
 'Namibia',
 'Netherlands',
 'New Zealand',
 'Norway',
 'Panama',
 'Poland',
 'Portugal',
 'Romania',
 'Singapore',
 'Slovakia',
 'Slovenia',
 'South Africa',
 'South Korea',
 'Spain',
 'Suriname',
 'Sweden',
 'Switzerland',
 'Taiwan Province of China',
 'Trinidad and Tobago',
 'United Kingdom',
 'United States',
 'Uruguay'}

For example Kenya’s not democratically positive at any point

#Apparently Kenya isn't democratic enough
df9[df9['country'] == 'Kenya'].groupby('year')['Democratic Quality'].agg(['max']).plot(kind='bar')

Take one that should be positive, the US is considered does have a positive index all through

df9[df9['country'] == 'United States'].groupby('year')['Democratic Quality'].agg(['max']).plot(kind='bar')

And taking one that shouldn’t be considered democratic, Saudi Arabia is negative and only by a margin of 0.4

df9[df9['country'] == 'Saudi Arabia'].groupby('year')['Democratic Quality'].agg(['max']).plot(kind='bar')

Getting to why some countries are ranked as undemocratic by this, this is wikipedia Democracy index which ranks multiple aspects of the Democracy like the Electoral process, Functioning of government etc and also a category. US is classified under the category of “Flawed Democracy” even though it has a high rank as we saw from the separate data, Kenya on the other hand is classified as “Hybrid regime” with low scores on the various aspects of a Democracy. At the bottom there’s “Authoritarian” and at the top “ Full democracy” and this info seems to support the observed results from this excel data.

I can also plot this for the most recent year with most entries, 2016 per country starting with positively indexed countries

df9[(df9['Democratic Quality'] > 0) & (df9['year'] == 2016)][['country','Democratic Quality']].plot(x='country', y='Democratic Quality', kind='barh', figsize=(20,20))

And negatively indexed countries

df9[(df9['Democratic Quality'] < 0) & (df9['year'] == 2016)][['country','Democratic Quality']].plot(x='country', y='Democratic Quality', kind='barh', figsize=(20,20))

The next column is Delivery Quality of the government, this also has a positive and negative index.

df9.groupby('year')['Delivery Quality'].agg(['max', 'min']).plot(kind='bar')

So doing the same as the last to view per country activity for 2016 starting with the positively indexed

df9[(df9['Delivery Quality'] > 0) & (df9['year'] == 2016)][['country','Delivery Quality']].plot(x='country', y='Delivery Quality', kind='barh', figsize=(20,20), legend=False)

And the negatively indexed countries

df9[(df9['Delivery Quality'] < 0) & (df9['year'] == 2016)][['country','Delivery Quality']].plot(x='country', y='Delivery Quality', kind='barh', figsize=(20,20), legend=False, secondary_y=False)

One final example is if you end up polling data and there’s a column for world it’s going to scew the graph being the longest bar by far, this is how I omit the row

df[df['Country'] != 'World'].plot(x='Country', y='Social support 2017', kind='barh', figsize=(20,40), legend=False)