2.1 Pclass
📌 그래프 그리기
f, ax = plt.subplots(1,2,figsize=(18,8)) # 도화지 준비
df_train['Survived'].value_counts().plot.pie(explode=[0,0.1],autopct='%1.1f%%', ax = ax[0],shadow=True)
ax[0].set_title('Pie plot - Survived') # 그래프 제목
ax[0].set_ylabel('') # 레이블을 없애겠다
sns.countplot('Survived', data=df_train, ax=ax[1])
ax[1].set_title('Count plot - Survived')
plt.show()
📌 Groupby
df_train[['Pclass','Survived']].groupby(['Pclass'], as_index=True).count()
- df_train[['Pclass','Survived']]
여러 컬럼을 가져올 때 리스트로 묶어서 가져온다는 것을 인지하고 [대괄호]를 잊지말자!
- as_index=True
Pclass를 인덱스로 둘거다 = True / 안둘거다 = False
pd.crosstab(df_train['Pclass'],df_train['Survived'],margins=True).style.background_gradient(cmap='summer_r')
df_train[['Pclass','Survived']].groupby(['Pclass'], as_index=True).mean().sort_values(by='Survived',ascending=False).plot.bar()
plt.show()
- 여기서 꼭 as_index=True여야함. 아니면 Pclass까지 그래프에 그려짐
📌 .unique()
df_train['Survived'].unique()
- 데이터에 고유값들이 어떠한 종류들이 있는지 알고 싶을때 사용하는 함수
📌 .nunique()
df_train['Survived'].nunique()
- 데이터에 고유값들의 총 수를 알고 싶을 때 출력해주는 함수
📌 .value_counts()
df_train['Survived'].value_counts()
- 값별로 데이터의 수를 출력해주는 함수
- 데이터의 고유값별로 몇개씩 들어있는지 알고 싶을때 유용한 함수
- 추가적으로 value_counts()는 기본적으로 내림차순으로 정렬
- 오름차순으로 정렬을 하고 싶다면 ascending=True 옵션을 지정
📌 그래프 그리기
# 각 클래스마다 인원수 counts
y_position = 1.02
f,ax = plt.subplots(1,2,figsize=(18,8))
df_train['Pclass'].value_counts().plot.bar(color=['#CD7F32','#FFDF00','#D3D3D3'],ax=ax[0])
ax[0].set_title('Number of passengers By Pclass', y=y_position)
ax[0].set_ylabel('Count')
sns.countplot('Pclass',hue='Survived', data=df_train, ax=ax[1])
# hue => hue는 해당 값으로 색 구분을 하는 역할(범례와 같은 역할)
ax[1].set_title('Pclass: Survived vs Dead', y=y_position)
plt.show()
'Study > kaggle' 카테고리의 다른 글
캐글 타이타닉 Titanic - 6. EDA - Embarked (0) | 2023.03.11 |
---|---|
캐글 타이타닉 Titanic - 5. Age, Sex, Pclass (violinplot) (0) | 2023.03.11 |
캐글 타이타닉 Titanic - 4. EDA - Age (2) | 2023.03.11 |
캐글 타이타닉 Titanic - 3. EDA - Sex(성별) (0) | 2023.03.08 |
캐글 타이타닉 Titanic 1. Dataset check (0) | 2023.03.06 |