从数据集中绘制比例
Plot proportion from Dataset
我正在尝试绘制来自 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))
你可以这样做:
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)))
我正在尝试绘制来自 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))
你可以这样做:
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)))