BIL.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. import warnings
  6. warnings.filterwarnings('ignore')
  7. data=pd.read_csv('D:/Ajay/input/Suicide.csv')
  8. data=data.drop(['HDI for year','country-year'],axis=1) #dropping these two columns
  9. #-----Table------------------------------------------------------------------------------------------------------------------------------------------------------------
  10. grouop_data=data.groupby(['age','sex'])['suicides_no'].sum().unstack() #collecting data and making table using 'unstack()' function
  11. grouop_data=grouop_data.reset_index().melt(id_vars='age') #arranging according to age
  12. grouop_data_female=grouop_data.iloc[:6,:] #retrieving 6 rows using 'iloc' function
  13. print("\n--Table of Suicides according to Female Age Groups--\n")
  14. from IPython.display import display
  15. display(grouop_data_female) #displaying table
  16. print("\n")
  17. #-----Country vs. suicide_no-------------------------------------------------------------------------------------------------------------------------------------------
  18. suicidesNo=[]
  19. for country in data.country.unique():
  20. suicidesNo.append(sum(data[data['country']==country].suicides_no)) #getting total no of suicides of all countries
  21. suicidesNo=pd.DataFrame(suicidesNo,columns=['suicides_no'])
  22. country=pd.DataFrame(data.country.unique(),columns=['country'])
  23. data_suicide_countr=pd.concat([suicidesNo,country],axis=1) #definind data and axis to plot
  24. data_suicide_countr=data_suicide_countr.sort_values(by='suicides_no',ascending=False)#displaying plot in descending order(i.e. from highest no. of suicides to lowest)
  25. sns.barplot(y=data_suicide_countr.country[:20],x=data_suicide_countr.suicides_no[:20]) #displaying bars of only 20 countries with highest no. of suicides
  26. plt.title("20 Countries with Higest Suicide Number from 1985 to 2016")
  27. plt.show()
  28. #-----Population vs. Age_group-----------------------------------------------------------------------------------------------------------------------------------------
  29. index_suicide=[]
  30. for age in data['age'].unique():
  31. index_suicide.append(sum(data[data['age']==age].suicides_no)/len(data[data['age']==age].suicides_no)) #getting suicide rate of each age group
  32. plt.bar(['5-14 years', '15-24 years', '25-34 years', '35-54 years', '55-74 years', '75+ years'],index_suicide,align='center',alpha=0.5) #defining xticks
  33. plt.xticks(rotation=45) #rotating xticks by 45 degree anticlockwise
  34. plt.title("Suicide rates of Different Age Groups")
  35. plt.show()