■ pivot_table 함수의 values/index/columns/aggfunc/fill_value/margins/margins_name 인자를 사용해 피벗 테이블 데이터를 만드는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import pandas as pd import numpy as np np.random.seed(0) category1List = ["수익", "비용"] category2Dictionary = {"수익" : ["영업수익", "영업외수익"], "비용" : ["영업비용", "영업외비용"]} category3Dictionary = { "영업수익" : ["제품매출", "용역매출" ], "영업외수익" : ["이자수익", "배당금수익"], "영업비용" : ["인건비" , "재료비" ], "영업외비용" : ["이자비용", "기부금" ] } sourceList = [] def generateAccountCode(대분류, 중분류, 소분류): return f"{대분류[:1]}{중분류[:1]}{소분류[:1]}" for category1 in category1List: for category2 in category2Dictionary[category1]: for category3 in category3Dictionary[category2]: accountCode = generateAccountCode(category1, category2, category3) for month in range(1, 13): targetAmount = np.random.randint(10000, 100000) actualAmount = np.random.randint(8000 , 120000) sourceList.append([category1, category2, category3, accountCode, f"2023-{month:02d}", targetAmount, actualAmount]) sourceDataFrame = pd.DataFrame(sourceList, columns = ["대분류", "중분류", "소분류", "계정코드", "해당월", "목표금액", "실적금액"]) pivotDataFrame = pd.pivot_table( sourceDataFrame, values = ["목표금액", "실적금액"], index = ["대분류", "중분류", "소분류"], columns = ["해당월"], aggfunc = "sum", fill_value = 0, margins = True, margins_name = "합계" ) |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.1.2 pandas==2.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.16.0 tzdata==2024.2 |
※ pip install pandas 명령을 실행했다.