<jupyter_start><jupyter_text>Total people killed in each country for each decade<jupyter_code>terror['Decade'] = (terror['Year']//10)*10
terror.Decade[:2]
# first get top 20 countries in terms of number of killings

top20killings = list(pd.DataFrame(terror.groupby(['Country']).sum()
                  ['Killed'].sort_values(ascending=False))
     [:20].reset_index()['Country'])
terror_country_killings = pd.DataFrame(terror.groupby(['Country', 'Decade']).sum()['Killed'].reset_index())
terror_country_killings20 = terror_country_killings[terror_country_killings.Country.isin(top20killings)]
df_stack = terror_country_killings20.pivot(index='Country', columns = 'Decade', values = 'Killed')
df_stack.fillna(value=0, inplace=True)
sns.set_style('ticks')

plt.figure(figsize=(12.5,7.5))

# plot heatmap
ax = sns.heatmap(df_stack.T,  cmap="OrRd", cbar_kws={'label': '# Killings'})

# turn the axis label
for item in ax.get_yticklabels():
    item.set_rotation(0)

for item in ax.get_xticklabels():
    item.set_rotation(90)

# save figure
plt.savefig('/Users/princegrover/Downloads/startbootstrap-creative-gh-pages_2/heatmap_ctr_dec.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Distribution of casualities<jupyter_code># plots a gaussian kernel density line also (smooth version of histogram)
sns.set_style('ticks')
plt.figure(figsize=(10, 5))
ax = sns.distplot(terror[(terror.casualities < 2000) & (terror.casualities > 100) ]['casualities'])
ax.set_title('Distribution of Casualities per attack ')
terror.casualities.hist()<jupyter_output><empty_output><jupyter_text>Activities of terrorist groups over the years top 10 terrorist groups in each region in each year  Decade in dropdown<jupyter_code>grp_dec = terror.groupby(['Group', 'Decade']).count()['Year'].reset_index()
grp_dec.rename(columns={'Year': 'Attacks'}, inplace=True)
grp_dec[:2]
grp_dec = grp_dec[grp_dec.Group != 'Unknown']
grp_dec.shape
data = []
for d in list(grp_dec.Decade.unique()):
    filtered_grp_dec = grp_dec[grp_dec.Decade == d]
    filtered_grp_dec = filtered_grp_dec.sort_values(by='Attacks', ascending=False)[:10]
    data.append(go.Bar(y = filtered_grp_dec.Group, 
                       x = filtered_grp_dec.Attacks, 
                       orientation = 'h'))
len(data)
for d in list(grp_dec.Decade.unique()):
    print(d)
updatemenus = list([
    dict(active=-1,
         x=-0.3,
         buttons=list([  
             
            dict(
                label = '1980s',
                 method = 'update',
                 args = [{'visible': [True, False, False, False, False, False]}, 
                     {'title': '10 Most Active Terrorist Groups in 1980s'}]),
             
             dict(
                  label = '1990s',
                 method = 'update',
                 args = [{'visible': [False, True, False, False, False, False]},
                     {'title': '10 Most Active Terrorist Groups in 1990s'}]),

            dict(
                 label = '2000s',
                 method = 'update',
                 args = [{'visible': [False, False, True, False, False, False]},
                     {'title': '10 Most Active Terrorist Groups in 2000s'}]), 
             
                         dict(
                label = '1970s',
                 method = 'update',
                 args = [{'visible': [False, False, False, True, False, False]}, 
                     {'title': '10 Most Active Terrorist Groups in 1970s'}]),
             
             dict(
                  label = '2010s',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, True, False]},
                     {'title': '10 Most Active Terrorist Groups in 2010s'}])
        ]),
    )
])
layout = dict(title='10 Most Active Terrorist Groups in Selected decade (Select from Dropdown)', showlegend=False,
              updatemenus=updatemenus,
              width=1100,
              height=500, 
              xaxis=dict(title='Number of Attacks'))

fig = dict(data=data, layout=layout)

# py.plot(fig)
plot(fig, filename='/Users/princegrover/Documents/msan/data_viz/group_dec.html' )<jupyter_output><empty_output><jupyter_text>Region in dropdown<jupyter_code>grp_reg = terror.groupby(['Group', 'Region']).count()['Year'].reset_index()
grp_reg.rename(columns={'Year': 'Attacks'}, inplace=True)
grp_reg[:2]
grp_reg = grp_reg[grp_reg.Group != 'Unknown']
data = []
for d in list(grp_reg.Region.unique()):
    filtered_grp_reg = grp_reg[grp_reg.Region == d]
    filtered_grp_reg = filtered_grp_reg.sort_values(by='Attacks', ascending=False)[:10]
    data.append(go.Bar(y = filtered_grp_reg.Group,
                       x = filtered_grp_reg.Attacks,
                       orientation = 'h'))
len(data)
for d in list(grp_reg.Region.unique()):
    print(d)
updatemenus = list([
    dict(active=-1,
         x=-0.3,
         buttons=list([  
             
            dict(
                label = 'Western Europe',
                 method = 'update',
                 args = [{'visible': [True, False, False, False, False, False,
                                     False, False, False, False, False, False]}, 
                     {'title': '10 Most Active Terrorist Groups in Western Europe'}]),
             
             dict(
                  label = 'East Asia',
                 method = 'update',
                 args = [{'visible': [False, True, False, False, False, False, 
                                      False, False, False, False, False, False]},
                     {'title': '10 Most Active Terrorist Groups in East Asia'}]),

            dict(
                 label = 'Middle East & North Africa',
                 method = 'update',
                 args = [{'visible': [False, False, True, False, False, False,
                                      False, False, False, False, False, False]},
                     {'title': '10 Most Active Terrorist Groups in Middle East & North Africa'}]), 
             
                         dict(
                label = 'South America',
                 method = 'update',
                 args = [{'visible': [False, False, False, True, False, False, 
                                      False, False, False, False, False, False]}, 
                     {'title': '10 Most Active Terrorist Groups in South America'}]),
             
             dict(
                  label = 'Central America & Caribbean',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, True, False, 
                                     False, False, False, False, False, False]},
                     {'title': '10 Most Active Terrorist Groups in Central America & Caribbean'}]),
                         dict(
                label = 'Sub-Saharan Africa',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, True,
                                      False, False, False, False, False, False]}, 
                     {'title': '10 Most Active Terrorist Groups in Sub-Saharan Africa'}]),
             
             dict(
                  label = 'North America',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, False,
                                      True, False, False, False, False, False]},
                     {'title': '10 Most Active Terrorist Groups in North America'}]),

            dict(
                 label = 'Eastern Europe',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, False,
                                      False, True, False, False, False, False ]},
                     {'title': '10 Most Active Terrorist Groups in Eastern Europe'}]), 
             
                         dict(
                label = 'South Asia',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, False, 
                                      False, False, True, False, False, False]}, 
                     {'title': '10 Most Active Terrorist Groups in South Asia'}]),
             
             dict(
                  label = 'Southeast Asia',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, False, 
                                      False, False, False, True, False, False ]},
                     {'title': '10 Most Active Terrorist Groups in Southeast Asia'}]), 
                                     dict(
                label = 'Central Asia',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, False, 
                                      False, False, False, False, True, False]}, 
                     {'title': '10 Most Active Terrorist Groups in Central Asia'}]),
             
             dict(
                  label = 'Australasia & Oceania',
                 method = 'update',
                 args = [{'visible': [False, False, False, False, False, False, 
                                      False, False, False, False, False, True ]},
                     {'title': '10 Most Active Terrorist Groups in Australasia & Oceania'}])
        ]),
    )
])
layout = dict(title='10 Most Active Terrorist Groups in selected Region (Select from Dropdown)', showlegend=False,
              updatemenus=updatemenus, width=1100,
              height=500, 
              xaxis=dict(title='Number of Attacks'))

fig = dict(data=data, layout=layout)

# py.plot(fig)
plot(fig, filename='/Users/princegrover/Documents/msan/data_viz/group_reg.html' )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12744895854815425, 'fraction_numerical': 0.014745308310991957, 'mean_word_length': 1.843447669305189, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '591198', 'license': 'MIT', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2607, 'n_words': 701, 'path': 'barplots_heatmap.ipynb', 'repo_name': 'neerjad/DataVisualization-FinalProject', 'size': '9698'}