text_plain_output_1.png
from collections import Counter

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

import warnings

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import seaborn as sns
from collections import Counter
import warnings
warnings.filterwarnings('ignore')
import os

suicide_rates = pd.read_csv('/kaggle/input/suicide-rates-overview-1985-to-2016/master.csv')

suicide_rates.columns

def detect_outliers(df, columnNames):
    outlier_indices = []
    for c in columnNames:
        Q1 = np.percentile(df[c], 25)
        Q3 = np.percentile(df[c], 75)
        IQR = Q3 - Q1
        outlier_step = IQR * 1.5
        outlier_list_col = df[(df[c] < Q1 - outlier_step) | (df[c] > Q3 + outlier_step)].index
        outlier_indices.extend(outlier_list_col)
    outlier_indices = Counter(outlier_indices)
    multiple_outliers = list((i for i, v in outlier_indices.items() if v > 1))
    return multiple_outliers

suicide_rates.loc[detect_outliers(suicide_rates, ['population', 'gdp_per_capita ($)', 'suicides/100k pop'])]

suicide_rates = suicide_rates.drop(detect_outliers(suicide_rates, ['population', 'gdp_per_capita ($)', 'suicides/100k pop']), axis=0).reset_index(drop=True)

year_list = list(suicide_rates.year.unique())
year_suicide_ratio = []
for i in year_list:
    a = suicide_rates[suicide_rates['year'] == i]
    year_suicide_rate = sum(a.index) / len(a)
    year_suicide_ratio.append(year_suicide_rate)
data = pd.DataFrame({'year_list': year_list, 'year_suicide_ratio': year_suicide_ratio})
new_index = data['year_suicide_ratio'].sort_values(ascending=True).index.values
sorted_data = data.reindex(new_index)
plt.figure(figsize=(15, 10))
sns.barplot(x=sorted_data['year_list'], y=sorted_data['year_suicide_ratio'])
plt.xticks(rotation=90)
plt.xlabel('Years')
plt.ylabel('Suicides')
plt.title('Which year did the most suicides occour')