[PYTHON/PANDAS] DataFrame 클래스 : cumsum 메소드를 사용해 누적 합계 구하기
■ DataFrame 클래스의 cumsum 메소드를 사용해 누적 합계를 구하는 방법을 보여준다. ▶ 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import pandas as pd import numpy as np import matplotlib.pyplot as plt datetimeIndex = pd.date_range("2000/01/01", periods = 1000) dataFrame1 = pd.DataFrame( np.random.randn(1000, 4), index = datetimeIndex, columns = ["A", "B", "C", "D"] ) print(dataFrame1) print() """ A B C D 2000-01-01 1.499520 -0.628333 -1.435202 0.167799 2000-01-02 -0.591156 -2.497511 1.069914 -2.325316 2000-01-03 -1.073116 -1.088665 1.397093 0.932822 2000-01-04 -0.469965 -0.413086 -1.022118 -0.991445 2000-01-05 0.449823 -0.029230 -0.934880 0.413968 ... ... ... ... ... 2002-09-22 3.135395 -0.130802 -0.730063 0.581774 2002-09-23 -0.627195 -0.640609 0.678296 -0.741038 2002-09-24 -0.095009 -0.509879 -0.754776 -1.355951 2002-09-25 -1.322082 0.110753 -0.686825 -0.849719 2002-09-26 -0.691853 1.422983 -2.265978 -1.229776 [1000 rows x 4 columns] """ dataFrame2 = dataFrame1.cumsum() print(dataFrame2) """ A B C D 2000-01-01 1.499520 -0.628333 -1.435202 0.167799 2000-01-02 0.908364 -3.125844 -0.365288 -2.157517 2000-01-03 -0.164752 -4.214509 1.031805 -1.224695 2000-01-04 -0.634718 -4.627595 0.009687 -2.216140 2000-01-05 -0.184895 -4.656825 -0.925193 -1.802172 ... ... ... ... ... 2002-09-22 -60.715338 -52.075193 -26.766710 15.256986 2002-09-23 -61.342533 -52.715801 -26.088414 14.515948 2002-09-24 -61.437541 -53.225681 -26.843190 13.159998 2002-09-25 -62.759624 -53.114928 -27.530015 12.310278 2002-09-26 -63.451477 -51.691945 -29.795993 11.080502 [1000 rows x 4 columns] """ dataFrame2.plot() plt.show() |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
contourpy==1.3.0 cycler==0.12.1 fonttools==4.54.1 kiwisolver==1.4.7 matplotlib==3.9.2 numpy==2.1.3 packaging==24.2 pandas==2.2.3 pillow==11.0.0 pyparsing==3.2.0 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.16.0 tzdata==2024.2 |
※ pip install pandas matplotlib