이전 분석 결과 '나이'와 생존율의 관계가 미미한 수치로 나왔는데,
이번에는 나이대별로 생존율과의 관계를 더 깊이 알아가보자.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
numpy란?
데이터 연산을 빠르고 쉽게 할 수 있게 도와주는 라이브러리
seaborn 이란?
matplotlib으로 부터 만들어진 데이터 시각화를 위한 라이브러리
랜덤으로 분포되어 있는 데이터를 시각화 할때 주로 사용
titanic = pd.read_table('파일경로',sep=',')
print(titanic.isnull().sum())
titanic = titanic.dropna()
titanic.describe()

#나이별로 히스토그램 구하기
titanic['Age'].hist(bins=40,figsize=(18,8),grid=True)

#나이별 구분 및 각 나이별 생존율 확인 하기
titanic['Age_cat'] = pd.cut(titanic['Age'],bins=[0,3,7,15,30,60,100],include_lowest=True,labels=['baby','children','teenage','young','adult','old'])
#연령대를 기준으로 평균 값을 구해 볼수 있어요!
titanic.groupby('Age_cat').mean()

#그래프 크기 설정
plt.figure(figsize=(14,5))
# 바 그래프 그리기 (x축 = Age_cat, y축 = Survived)
sns.barplot(x='Age_cat',y='Survived',data=titanic)
# 그래프 나타내기
plt.show()

나이대를 나누어 생존율과의 관계를 살펴보니,
baby 와 children의 생존율이 다른 연령대에 비해 매우 높은 것을 확인 할 수 있었다.