从数据集中绘制比例

时间:2016-11-12 11:35:22

标签: python pandas matplotlib dataframe kaggle

我试图用Kaggle绘制泰坦尼克号数据的年龄分布比例。

age_distribution_died= df.Age[df['Survived']==0].dropna().value_counts().sort_index()
age_distribution_survived=df.Age[df['Survived']==1].dropna().value_counts().sort_index()

我想要做的是将它们分组到10个大小的容器中,因此对于0-10岁,10-20岁等我尝试使用此代码,但它不起作用:

bins = [0,10,20,30,40,50,60,70,80]
test = age_distribution.groupby(pd.cut(age_distribution,bins))

1 个答案:

答案 0 :(得分:1)

你可以这样做:

import matplotlib
matplotlib.style.use('ggplot')

df = pd.read_csv(r'D:\download\train.csv')

clean = df.dropna(subset=['Age'])

(clean.groupby(pd.cut(clean.Age, np.arange(0, 90, step=10)))
      .Survived.mean().mul(100)
      .to_frame('Survival rate')
      .plot.bar(rot=0, width=0.85, alpha=0.5, figsize=(14,10)))

enter image description here