【问题标题】:Plot proportion from Dataset从数据集中绘制比例
【发布时间】:2017-03-26 12:54:58
【问题描述】:

我正在尝试绘制来自 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))

【问题讨论】:

  • 您能向我们展示执行代码的输出/回溯吗?这让我们更容易提供帮助。

标签: python pandas matplotlib dataframe kaggle


【解决方案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)))

【讨论】:

  • @AlenPavlović,不客气。如果您认为 accepting 回答了您的问题,请考虑回答
猜你喜欢
  • 2019-02-10
  • 2011-01-19
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多