# 컬럼별 결측치 갯수 확인
weather_df.isna().sum()
'''
최저기온(°C) 6
최고기온(°C) 6
일강수량(mm) 6
최대 순간 풍속(m/s) 6
평균 풍속(m/s) 8
최대 순간 풍속 풍향(hhmi) 6
'''
# Boolean -> Boolean Indexing : 결측치 위치 확인
weather_df['최저기온(°C)'][weather_df['최저기온(°C)'].isna()]
'''
일시
2010-12-08 NaN
2010-12-09 NaN
2010-12-10 NaN
2010-12-11 NaN
2010-12-12 NaN
2010-12-13 NaN
'''
시간 흐름에 따른 데이터 -> 시계열
비어있는 값 앞뒤로 데이터 경향을 파악
weather_df.loc['2010-12-01':'2010-12-31']
# 1. 일강수량의 결측치를 0으로 채우기
weather_df['일강수량(mm)'] = weather_df['일강수량(mm)'].fillna(0)
weather_df['일강수량(mm)'].isna().sum() # 값이 채워졌는지 확인 -> 0
# 2. 최저기온, 최고기온의 결측치를 사분위값 50% 값으로 채우기
weather_df.loc['2010-12-05':'2010-12-15'].describe()
weather_df['최저기온(°C)'] = weather_df['최저기온(°C)'].fillna(0.4)
weather_df['최고기온(°C)'] = weather_df['최고기온(°C)'].fillna(2.9)
# 3. 최대 순간 풍속의 결측치를 평균값으로 채우기
weather_df.loc['2010-12-06':'2010-12-15'].describe()
weather_df['최대 순간 풍속(m/s)'] = weather_df['최대 순간 풍속(m/s)'].fillna(24.1)
# 4. 평균 풍속의 12월 4일부터 18일까지 결측치를 평균값으로 채우기
weather_df['2010-12-04':'2010-12-18'].describe()
weather_df['평균 풍속(m/s)'] = weather_df['평균 풍속(m/s)'].fillna(4.642857)
# 5. 최대 순간 풍속 풍향의 12월 7일부터 17일까지 결측치를 평균값으로 채우기
weather_df.loc['2010-12-07':'2010-12-17'].describe()
weather_df['최대 순간 풍속 풍향(hhmi)'] = weather_df['최대 순간 풍속 풍향(hhmi)'].fillna(305.38)
weather_df.info() # non-null 확인
데이터 시각화
!pip install koreanize-matplotlib # 한글 출력을 위한 추가 라이브러리
import matplotlib.pyplot as plt
import koreanize_matplotlib
# 그래프 변수
x = weather_df.index # 시간순서
y1 = weather_df['최저기온(°C)']
y2 = weather_df['최고기온(°C)']
y3 = weather_df['일강수량(mm)']
y4 = weather_df['최대 순간 풍속(m/s)']
y5 = weather_df['평균 풍속(m/s)']
y6 = weather_df['최대 순간 풍속 풍향(hhmi)']
# 두 기온 그래프를 하나의 그래프 안에 표시
plt.figure(figsize=(4, 3))
plt.title('월별 기온')
plt.xlabel('월')
plt.ylabel('기온')
plt.plot(x_month, month_low_temp, '--b', label='최저기온')
plt.plot(x_month, month_high_temp, 'o-r', label='최고기온')
plt.legend(loc='upper right')
plt.show()
# 일강수량 그래프를 바 차트로 나타내기 -> 데이터 타입에 따라 적절한 그래프 선택하기
plt.figure(figsize=(4, 3))
plt.title('월별 일강수량')
plt.ylabel('월')
plt.xlabel('강수량')
# plt.barh(y, x)
plt.barh(x_month, month_daily_vol)
plt.show()
# 1월 데이터 -> groupby 필요없음. 비교가 아니고 조건
jan = weather_df[weather_df['월']==1]
x_day = jan.index.day
# 1월 데이터를 subplot 이용해서 2x3 으로 그려주세요. -> 강수량은 BAR 그래프로
plt.figure(figsize=(14, 8))
# subplots 간의 간격 조절
plt.subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.35)
plt.subplot(2, 3, 1)
plt.title('최저기온')
plt.plot(jan['최저기온(°C)'])
plt.subplot(2, 3, 2)
plt.title('최고기온')
plt.plot(jan['최고기온(°C)'])
plt.subplot(2, 3, 3)
plt.title('일강수량')
plt.bar(x_day, jan['일강수량(mm)']) # 바 차트는 x, y를 입력해야함
plt.subplot(2, 3, 4)
plt.title('최대 순간 풍속')
plt.plot(jan['최대 순간 풍속(m/s)'])
plt.subplot(2, 3, 5)
plt.title('평균 풍속')
plt.plot(jan['평균 풍속(m/s)'])
plt.subplot(2, 3, 6)
plt.title('최대 순간 풍속 풍향')
plt.plot(jan['최대 순간 풍속 풍향(hhmi)'])
plt.show()