I want to find the number of suicides that occurred from year 1985-2016. Which i did easily
by
df.groupby('year').suicides_no.sum()
Now i want to break down the total number of suicides happened per year further into male and female.
The column name is ‘sex’ which contains two values – male , female.
>Solution :
Simply add a further group (long format):
df.groupby(['year', 'sex'])['suicides_no'].sum()
Or use a crosstab (wide format):
pd.crosstab(df['year'], df['sex'], values=df['suicides_no'], aggfunc='sum')