2016-11-12 5 views
0

카글에서 타이타닉 데이터의 연령 분포에 대한 비율을 플롯하려합니다. 이 코드로 시도 등 연령 0 ~ 10, 10 ~ 20에 대한 있도록데이터 세트의 플롯 비율

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의 쓰레기통에 그룹화하는 것입니다

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

당신이 우리에게 코드를 실행 출력/역 추적을 보여줄 수 있습니까? 그것은 우리가 더 쉽게 도와줍니다. –

답변

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

+0

@ AlenPavlović, 여러분은 환영합니다. 질문에 대답했다고 생각되면 [accepting] (http://meta.stackexchange.com/a/5235) 답을 생각하십시오. – MaxU