한국전력공사_전기에너지 데이터

ewillwin·2023년 1월 30일
0

TSMtech Record

목록 보기
37/39

한국전력공사에서 월별&법정동별 전기에너지소비량 데이터를 제공하고 있음을 확인하여 test를 진행함

Data structure

  • 법정동주소 column에서 '리'까지 표시되어있음 & 시군구 column에서 공백이 존재함/ 법정동코드로 변환하기 위해 '리'를 제거함 & 공백을 제거함
  • 세종특별자치시의 경우 따로 처리가 필요함
  • 주소의 최소 단위가 법정동이기 때문에 행정동으로 변환할 때, 1대 1 매핑이 되는 경우에만 변환이 가능함
  • 월별로 column name 들이 조금씩 상이함/ 순서 또한 다름
  • 계약종별에서 가로등과 농사용은 건물에너지가 아니라고 판단하여 제외함

Data processing Code

아직 미완

from urllib.request import urlopen
import boto3
from typing import final
from venv import create
import pandas as pd
from multiprocessing import Process
import bisect
import pandas as pd
from typing import final
from venv import create
from decimal import Decimal
import shutil
import pandas
import re

def code_truc(s):
    tmp = s[:8]
    ans = tmp + "00"
    return ans

##### map1 open #####
map1 = pd.read_excel("C:/Users/TSM/OneDrive/바탕 화면/alladdr/map1.xlsx",names=['행정동코드','법정동코드'])
map1 = map1.astype({'행정동코드':'str','법정동코드':'str'})
map1_set = set(list(map1['법정동코드']))

##### code map open #####
codemap = pd.read_csv("C:/Users/TSM/Downloads/국토교통부_전국 법정동_20221031.csv", header=1, names=['법정동코드','시도명','시군구명','읍면동명','리명','순위','생성일자','삭제일자','과거법정동코드'], encoding='cp949')
codemap = codemap[['법정동코드','시도명','시군구명','읍면동명']]
codemap = codemap.astype({'법정동코드':'str','시도명':'str','시군구명':'str','읍면동명':'str'})
codemap['코드'] = codemap['법정동코드'].apply(code_truc)
codemap = codemap[['코드','시도명','시군구명','읍면동명']]
codemap['주소'] = 0
for idx, row in codemap.iterrows():
    s = row['시도명'] + ' ' + row['시군구명'] + ' ' + row['읍면동명']
    codemap.at[idx, '주소'] = s
codemap = codemap[['코드', '주소']]
codemap = codemap.drop_duplicates(subset='코드', keep='first')
codemap_set = set(list(codemap['주소']))

filename = ['201301-06','201307-12','201401-06','201407-12','201501-06','201507-12','201601-06','201607-12','201701-06','201707-12','201801-06','201807-12','201901-06','201907-12','202001-06','202007-12','202101-06','202107-10','202111','202112','202201','202202','202203','202204','202205','202206','202207','202208']
for i in range(len(filename)):
    df = pd.read_excel("C:/Users/TSM/OneDrive/바탕 화면/한국전력공사_전기에너지/" + filename[i] + ".xlsx")
    df = df.fillna(0)

    ##### elcty list에 row insert #####
    elcty = []
    for idx, row in df.iterrows():
        if row[5] != '가로등' and row[5] != '농사용' and row[6] != '5호미만제거':
            month = str(row[1])
            month_str = ''
            if len(month) != 2:
                month_str = '0' + month
            else:
                month_str = month
            elcty.append([str(row[0]) + month_str, str(row['시도']), str(row['시군구']), str(row[4]), str(row[5]), row[6], row[7]]) #[날짜, 시도, 시군구, 읍면동, 유형, 호수, 전기에너지]
    print("elcty 생성 완료.")

    ##### 주소를 (법정동)코드로 변환 #####
    elcty_code = []
    for a in range(len(elcty)):
        index_tmp = elcty[a][3].find(' ')
        if index_tmp != -1 : # 읍면동에서 '리' 없애기
            sss = elcty[a][3][:index_tmp]
            elcty[a][3] = sss

        sss = elcty[a][2].replace(' ', '') # 시군구에서 공백 없애기
        elcty[a][2] = sss

        addr = elcty[a][1] + ' ' + elcty[a][2] + ' ' + elcty[a][3]
        code = ''
        if addr in codemap_set:
            code = codemap.loc[codemap['주소'] == addr]['코드'].values.astype('str')[0]
        else:
            print(addr)
            print("법정동코드 변환에서 예외 발생")
        elcty_code.append([elcty[a][0], code, elcty[a][4], elcty[a][5], elcty[a][6]]) #[날짜, (법정동)코드, 유형, 호수, 전기에너지]
    print("elcty_code 생성 완료.")

    ##### 법정동 행정동 매핑 #####
    elcty_hjd = []
    for x in range(len(elcty_code)):
        if elcty_code[x][3] in map1_set:
            elcty_hjd.append([elcty_code[x][0], map1.loc[map1['법정동코드']==elcty_code[x][1]]['행정동코드'].values.astype('str')[0], elcty_code[x][2], elcty_code[x][3], elcty_code[x][4]])
        else:
            continue
    print("elcty_hjd 생성 완료.")

    ##### elcty_hjd을 file로 추출 #####
    des = open("C:/Users/TSM/OneDrive/바탕 화면/한국전력공사_전기에너지/elcty_by_type.txt", 'a')
    for y in range(len(elcty_hjd)):
        line = str(elcty_hjd[y][0]) + ',' + str(elcty_hjd[y][1]) + ',' + str(elcty_hjd[y][2]) + ',' + str(elcty_hjd[y][3]) + ',' + str(int(elcty_hjd[y][4])) + '\n'
        des.write(line)
    des.close()
    print("file로 추출 완료.")
    print("==============================================================")
profile
Software Engineer @ LG Electronics

0개의 댓글