본문 바로가기

[중급] 가볍게 이것저것

[스마트 빌딩] 건물의 전력 소비량 알아보기_2

전력 소비 소스의 정보 다루기

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

building = pd.read_csv('data/COMBED/total_main.csv')
building.columns = ['time', 'Power']

dataset = ['1f', '2f', '3f', '4f', '5f', 
           'lift', 'ahu_0', 'ahu_1', 'ahu_2']

for i in dataset:
    path = 'data/COMBED/%s.csv' % i
    target = pd.read_csv(path)
    target.columns = ['time', i]
    building = pd.merge(building, target, how='outer')
building.describe()

time Power 1f 2f 3f 4f 5f lift ahu_0 ahu_1 ahu_2
count 8.085610e+05 82887.000000 86191.000000 86197.000000 86196.000000 86197.000000 86199.000000 82883.000000 8.619100e+04 8.619700e+04 8.619700e+04
mean 1.402898e+12 26953.199165 6414.705527 6243.521385 2565.171819 2270.086414 5868.598787 663.357962 1.108174e+03 1.175693e+03 1.645170e+03
std 7.445254e+08 12679.686055 2668.779318 3962.974497 937.462608 830.664546 5145.835523 753.209339 1.276242e+03 1.555851e+03 1.720803e+03
min 1.401595e+12 0.000000 0.000000 0.000000 764.284729 0.000000 0.000000 135.551239 1.200000e-17 1.200000e-17 0.000000e+00
25% 1.402249e+12 16527.759766 5399.188232 2394.531494 1784.570007 1633.765137 1022.516541 416.889313 1.200000e-17 1.200000e-17 2.000000e-17
50% 1.402904e+12 23836.597656 6409.180176 5742.391602 2639.118896 2318.925293 1615.134399 429.976685 1.200000e-17 1.200000e-17 2.000000e-17
75% 1.403538e+12 38319.457031 8430.785645 9529.845703 3330.486694 2859.600830 11339.651367 455.709259 2.407904e+03 2.807285e+03 3.363910e+03
max 1.404187e+12 58734.121094 18046.414062 17125.187500 5782.950195 6763.777344 16185.705078 9232.341797 4.367141e+03 4.559870e+03 4.406700e+03
building['datetime'] = building['time'].astype("datetime64[ms]")

# 이 데이터프레임의 인덱스를 ['datetime'] 컬럼의 값으로 바꿔줍니다.
# 시간을 기준으로 묶어주기 용이하기 때문입니다.
building.index = building['datetime']

# .groupby() 를 이용하여 특정 기준으로 데이터를 묶어 .mean()으로 평균값을 구합니다.
# 여기서 특정 기준이라 함은 pd.Grouper() 에서 시간을 기준으로 삼아주게 됩니다.
building = building.groupby(pd.Grouper(freq='H')).mean()
# building 데이터에 연, 월, 일, 시, 분, 초를 나타내는 새로운 컬럼을 생성합니다.
# 각각의 이름을 datetime-month/day/hour/minute/second라고 지정합니다.
# 이 컬럼에 날짜(datetime) 컬럼의 dt(datetime의 약자입니다) 옵션을 활용하여 월일시분을 따로 넣어줍니다.
building["datetime-month"] = building.index.month
building["datetime-day"] = building.index.day
building["datetime-hour"] = building.index.hour

# dayofweek는 날짜에서 요일(월~일)을 가져오는 기능입니다.
# 값은 0(월), 1(화), 2(수), 3(목), 4(금), 5(토), 6(일) 을 나타냅니다.
building["datetime-dayofweek"] = building.index.dayofweek
# building 변수에 할당된 데이터의 행렬 사이즈를 출력합니다.
# 출력은 (row, column) 으로 표시됩니다.
print(building.shape)

# .head()로 building 데이터의 상위 5개를 띄우되,
# datetime과 이와 연관된 나머지 다섯 개의 컬럼만을 출력합니다.
building[["datetime-month", "datetime-day", 
       "datetime-hour", "datetime-dayofweek"]].head()
(720, 15)

datetime-month datetime-day datetime-hour datetime-dayofweek
datetime
2014-06-01 04:00:00 6 1 4 6
2014-06-01 05:00:00 6 1 5 6
2014-06-01 06:00:00 6 1 6 6
2014-06-01 07:00:00 6 1 7 6
2014-06-01 08:00:00 6 1 8 6
# matplotlib의 subplots를 사용합니다. 이 함수는 여러 개의 시각화를 한 화면에 띄울 수 있도록 합니다.
# 이번에는 2x2으로 총 4개의 시각화를 한 화면에 띄웁니다.
figure, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)

# 시각화의 전체 사이즈는 18x8로 설정합니다.
figure.set_size_inches(18, 8)

# seaborn의 barplot으로 subplots의 각 구역에
# 건물 전체, 2층, 5층, 4층의 전력 소비량을 출력합니다.
p1 = building.pivot_table(index = 'datetime-dayofweek', columns = 'datetime-hour', values = 'Power')
p2 = building.pivot_table(index = 'datetime-dayofweek', columns = 'datetime-hour', values = '2f')
p3 = building.pivot_table(index = 'datetime-dayofweek', columns = 'datetime-hour', values = '5f')
p4 = building.pivot_table(index = 'datetime-dayofweek', columns = 'datetime-hour', values = '4f')

sns.heatmap(p1, ax=ax1)
sns.heatmap(p2, ax=ax2)
sns.heatmap(p3, ax=ax3)
sns.heatmap(p4, ax=ax4)
<matplotlib.axes._subplots.AxesSubplot at 0x144807dea90>