数据集:自杀率,目标:分析数据

(7)自杀率与男女的关系
grouop_data = data.groupby(['Age','Gender'])['SuicidesNo'].sum().unstack()
grouop_data = grouop_data.reset_index().melt(id_vars='Age')
grouop_data_female = grouop_data.iloc[:6,:]
grouop_data_male = grouop_data.iloc[6:,:]

female_=[175437,208823,506233,16997,430036,221984]
male_=[633105,915089,1945908,35267,1228407,431134]
plot_id = 0
for i,age in enumerate(['15-24 years','25-34 years','35-54 years','5-14 years','55-74 years','75+ years']):
    plot_id += 1
    plt.subplot(3,2,plot_id)
    plt.title(age)
    fig,ax = plt.gcf(),plt.gca()
    sns.barplot(x=['female','male'],y=[female_[i],male_[i]])
    plt.tight_layout()#减少堆叠
    fig.set_size_inches(10,15)
grouop_data = data.groupby(['Age','Gender'])['SuicidesNo'].sum()

Age          Gender
15-24 years  female     175437
             male       633105
25-34 years  female     208823
             male       915089
35-54 years  female     506233
             male      1945908
5-14 years   female      16997
             male        35267
55-74 years  female     430036
             male      1228407
75+ years    female     221984
             male       431134
grouop_data = data.groupby(['Age','Gender'])['SuicidesNo'].sum().unstack()#把男女转换成列

Gender          Age  female     male
0       15-24 years  175437   633105
1       25-34 years  208823   915089
2       35-54 years  506233  1945908
3        5-14 years   16997    35267
4       55-74 years  430036  1228407
5         75+ years  221984   431134



grouop_data = grouop_data.reset_index().melt(id_vars='Age')

Age  Gender    value
0   15-24 years  female   175437
1   25-34 years  female   208823
2   35-54 years  female   506233
3    5-14 years  female    16997
4   55-74 years  female   430036
5     75+ years  female   221984
6   15-24 years    male   633105
7   25-34 years    male   915089
8   35-54 years    male  1945908
9    5-14 years    male    35267
10  55-74 years    male  1228407
11    75+ years    male   431134



结果:

参考:https://www.kaggle.com/kralmachine/data-visualization-of-suicide-rates

(8)每个年龄段人口

index_population=[]
for age in data['Age'].unique():
    index_population.append(sum(data[data['Age']==age].Population/len(data[data['Age']==age].Population)))
plt.bar(['15-24 years','35-54 years','75+ years','25-34 years','55-74 years','5-14 years'],index_population,align='center',alpha=0.5)
plt.xticks(rotation=90)
plt.show()

结果:

(9)自杀率与年份的关系

sns.set_color_codes('muted')
sns.barplot(x='Year',y='SuicidesNo',data=data,
            label='Year Suicide',color='y')
plt.xticks(rotation=90)
plt.show()

结果:

话说2016发生了什么减少了这么多.....

(10)

fig=sns.jointplot(y='SuicidesNo',x='Year',data=data)
plt.show()

结果:

fig=sns.jointplot(y='SuicidesNo',x='Population',data=data)
plt.show()

sns.countplot(x='Generation',hue='Gender',data=data)
plt.xticks(rotation=45)
plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_43615654/article/details/89434969
今日推荐