<jupyter_start><jupyter_text><NAME>Task 4 - Perform ‘Exploratory Data Analysis’ on dataset ‘Global Terrorism’.<jupyter_code>#Import all the required libraries
import pandas as pd
read csv()
from sklearn.impute import SimpleImputer
import numpy as np
import plotly.express as px
import pycountry
import os
#Import the dataset
data = pd.read_csv("C:\\Users\\Admin\\Downloads\\Global Terrorism\\data.csv", encoding = 'ISO-8859-1')
pd.set_option('max_columns', None)
pd.set_option('max_rows', None)
data.head()
#finding info of the dataset
data.shape
#Removing any duplicate values
data.drop_duplicates(inplace=True)<jupyter_output><empty_output><jupyter_text>Missing values<jupyter_code>#show any missing values
data.isnull().sum().reset_index().sort_values(by=[0], ascending = False)
# showing missing values in percentages in descending order.
percent_missing = data.isnull().sum() * 100 / len(data)
missing_value_df = pd.DataFrame({'column_name': data.columns,
                                 'percent_missing': percent_missing})
missing_value_df.sort_values('percent_missing', inplace=True, ascending = False)
missing_value_df
#Remove columns with more than 25% missing data
morethan25_per = missing_value_df[missing_value_df['percent_missing']>23]
morethan25_per
# Removing columns with more than 25% missing value.
morethan25_per = missing_value_df[missing_value_df['percent_missing']>23]
morethan25_per
df = data.drop(morethan25_per.index, axis=1)
df.head()
df.shape
#for the rest of missing values we fill mean value
small_missing_values = df.isnull().sum().reset_index().sort_values(by=[0], ascending = False)
small_missing_values = small_missing_values[small_missing_values[0]!=0]
small_missing_values
#seperate object missing value and numerical missing value
lst = small_missing_values['index'].tolist()
object_missing_cols = []
num_missing_cols = []
for i in lst:
    if((df['{}'.format(i)]).dtype=='object'):
        object_missing_cols.append('{}'.format(i))
    else:
        num_missing_cols.append('{}'.format(i))
print(object_missing_cols)        
print(num_missing_cols)
df[object_missing_cols].head()

df[num_missing_cols].head()
#for numerical missing value
imputer = SimpleImputer()
imputer = imputer.fit(df[num_missing_cols])
df[num_missing_cols] = imputer.transform(df[num_missing_cols])
df[num_missing_cols].head()

#drop rows for missing object values
df = df.dropna()
df.isnull().sum().sum()

df.head()
#Highest attack frequency by countries
df2 = df.country_txt.value_counts().reset_index().rename(columns={'index': 'Country Name'})
df2.columns.values[1] = 'Count'
df2.head()<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code># visualization on world map
fig = px.choropleth(df2,
                    color="Count", locations="Country ISO Code",
                    hover_name="Country Name",
                    color_continuous_scale=px.colors.sequential.Plasma)
fig.show()
# graph shows count of most attacks
fig = px.bar(df2.head(25), x='Country Name', y='Count',
             hover_data=['Count'], color='Count', height=400)
fig.show()
#Graph shows most Targeted group
target_type= df['targtype1_txt'].value_counts().reset_index().rename(columns={'index': 'Target Types'})
target_type.columns.values[1] = 'Count'

fig = px.bar(target_type, x='Target Types', y='Count',
             hover_data=['Count'], color='Count', height=400)
fig.show()
#Graph shows the most used weapons for attack
weapon_type= df['weaptype1_txt'].value_counts().reset_index().rename(columns={'index': 'Weapon Types'})
weapon_type.columns.values[1] = 'Count'

fig = px.bar(weapon_type, x='Weapon Types', y='Count',
             hover_data=['Count'], color='Count', height=400)
fig.show()


fig = px.pie(weapon_type, values='Count', names='Weapon Types',
             title='Weapon Types Attack',
             hover_data=['Count'])
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()
#Graph showsfrequency of attacks by year
attack_on_years= df['iyear'].value_counts().reset_index().rename(columns={'index': 'Year'})
attack_on_years.columns.values[1] = 'Count'

fig = px.bar(attack_on_years, x='Year', y='Count',
             hover_data=['Count'], color='Count', height=400)
fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14094746716697937, 'fraction_numerical': 0.013367729831144466, 'mean_word_length': 5.6536661466458655, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '359160', 'license': 'MIT', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1382, 'n_words': 293, 'path': 'TSF- Task#4.ipynb', 'repo_name': 'jayanthd001/TSF_Data-Science-and-Business-Analytics', 'size': '4267'}