[KT AIVLE SCHOOL]

파이썬 기초(8)

조진목_스터디 2024. 9. 15. 20:29

특정 열 데이터 조회

# total_bill 열 조회 / tip.loc[행, 열]
tip.loc[:,'total_bill']

 

 

조건으로 데이터 프레임 조회

# tip 열 값이 6.0 보다 큰 행 조회
# tip.loc[tip['tip'] > 6.0,:]
tip.loc[tip['tip'] > 6.0]

 

 

특정 열 특정 값 데이터 조회

# 값 나열 (day가 Sat 또는 Sun)
tip.loc[tip['day'].isin(['Sat', 'Sun'])]

 

 

조건에 만족하는 특정 열 조회

# 조건에 맞는 하나의 열 조회
tip.loc[tip['size'] >= 5, ['tip']]

 

 

그룹화 집계

# day별 tip 합계 --> 데이터프레임
tip.groupby('day', as_index=False)[['tip']].sum()

 

 

시각화

# 라이브러리 불러오기
import matplotlib.pyplot as plt

# day 별 tip 비교 시각화
plt.figure(figsize=(5, 3)) # 차트 크기 조절
plt.bar(tip_sum['day'], tip_sum['tip'], color='green')
plt.title('Tip by Day', size=15, pad=10) #size 크기, pad 그래프와 공백, fontweight='bold' 진하게
plt.show()

 

 

특정 선  포함 시각화

# tip 분포 시각화
tip_mean = tip['tip'].mean()
plt.hist(tip['tip'], bins=20) # bins에 따라서 그래프가 바뀜, alpha=.7 그래프 뿌옇게, ec='w' 테두리 색 흰색
plt.axvline(tip_mean, color='r') # 평균이 세로 선으로 출력
plt.show()

 

 

데이터프레임에서 숫자형 열만 합계 조회

# day + smoker별 나머지 열들 합계 조회
tip_sum = tip.groupby(by=['day', 'smoker'], as_index=False).sum(numeric_only=True)

# 확인
tip_sum

 

 

숫자형 자료 변환 후 시각화

 

 

시각화 import

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import koreanize_matplotlib
import warnings

warnings.filterwarnings(action='ignore')
%config InlineBackend.figure_format='retina'

 

 

날짜 데이터 전처리

 

 

마커 변경

 

 

여러 그래프 동시 출력

 

 

그래프 크기 조절

 

 

세로선, 가로선 출력

 

 

텍스트 추가

 

 

subplot(행 수 , 열 수, 위치)로 그래프 레이아웃

 

 

 

'[KT AIVLE SCHOOL]' 카테고리의 다른 글

파이썬 기초(10)  (0) 2024.09.22
파이썬 기초(9)  (0) 2024.09.19
파이썬 기초(7)  (0) 2024.09.12
파이썬 기초(6)  (0) 2024.09.11
파이썬 기초(5)  (0) 2024.09.10