aa_df = raw_df2[['newid','country','year','sr22cap','seltrans','sa1','sa2','sr0str','sr11cheat','sr21cheat','sr23viol','sr13viol']]
len(aa_df)

field = 'sr22cap'
viol_df = aa_df[['newid','country','year',field]]
viol_df = viol_df[viol_df[field] != ' ']

viol_df[field] = map(lambda x:int(x), viol_df[field])

adf2 = viol_df[viol_df[field] >= 1].groupby('country').max()
len(adf2)

"""
Explanation: i.  SR0STR – legal framework
ii. SR11CHEAT – pre-election cheating
iii.    SR13VIOL – pre-election violence
iv. SR21CHEAT – election-day cheating
v.  SR23VIOL – election-day violence
End of explanation
"""


itu_broad = pd.DataFrame.from_csv('Fixed_broadband_2000-2014_per100.csv')
itu_broad = itu_broad[['2000','2001','2002','2003','2004','2005','2006','2007',
                         '2008','2009','2010','2011','2012','2013','2014']]
itu_broad

b_stack = itu_broad.stack()

def get_country_series_b(country, stack):
    c1 = stack[country]
    c1.index = c1.index.map(lambda x:int(x))
#     c1 = c1.map(lambda x: int(x.replace(',','')))
    return c1
    
def plot_country_b(country, stack):
    s = get_country_series_b(country, stack)
    s.plot(title=country,xlim=[1999,2015])

for c in seltrans_df.country.unique():
    try:
        if(is_above(c,5)):
            plot_country_b(c, b_stack)
    except:
        continue

def is_above(country, base, stack):
    cstack = stack[country]
    return cstack.max() >= base

is_above("Algeria", 5, b_stack)


for c in seltrans_df.country.unique():
    try:
#         if(is_above(c,0.5*(10**8))):
        if(is_above(c,0.9*(10**8), m_stack)):
            plot_country(c, m_stack)
    except:
        continue

is_above("Afghanistan", 1000000000000, m_stack)
m_stack["Afghanistan"].max()

ldc = ["Angola",
"Madagascar",
"Benin",
"Malawi",
"Burkina Faso",
"Mali",
"Burundi",
"Mauritania",
"Central African Rep.",
"Mozambique",
"Chad",
"Niger",
"Comoros",
"Rwanda",
"Congo (Dem. Rep.)",
"S. Tome & Principe",
"Djibouti",
"Senegal",
"Equatorial Guinea",
"Sierra Leone",
"Eritrea",
"Somalia",
"Ethiopia",
"South Sudan",
"Gambia",
"Sudan",
"Guinea",
"Togo",
"Guinea-Bissau",
"Uganda",
"Lesotho",
"Tanzania",
"Liberia",
"Zambia",
"Afghanistan",
"Nepal",
"Bangladesh",
"Solomon Islands",
"Bhutan",
"Timor-Leste",
"Cambodia",
"Tuvalu",
"Kiribati",
"Vanuatu",
"Lao P.D.R.",
"Yemen",
"Myanmar",
"Haiti"]


v = 0
for c in ldc:
    try:
        str_val = m_stack[c]["2014"]
        str_val = str_val.replace(',','')
        v += int(str_val)
    except:
        print c

m_stack["Comoros"]["2014"]

v2 = v+383000

v2/float(932000000)

"""
Explanation: Fixed broadband subscriptions
End of explanation
"""


