■ DataFrame 클래스의 sub 메소드에서 axis 인자를 사용해 뺄셈하는 방법을 보여준다.
▶ 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 |
import pandas as pd import numpy as np datetimeIndex = pd.date_range("20130101", periods = 6) dataFrame1 = pd.DataFrame(np.random.randn(6, 4), index = datetimeIndex, columns = list("ABCD")) print(dataFrame1) """ A B C D 2013-01-01 1.727789 0.330913 0.705797 0.987456 2013-01-02 -1.074524 0.247558 0.989254 1.718233 2013-01-03 0.498621 0.341959 0.171567 -0.567567 2013-01-04 -1.395337 0.952481 -0.019128 0.993886 2013-01-05 1.593197 0.803565 -0.327899 1.062228 2013-01-06 -2.427016 0.580813 -1.242874 0.772521 """ print() series1 = pd.Series([1, 3, 5, np.nan, 6, 8], index = datetimeIndex) series2 = series1.shift(2) print(series2) """ 2013-01-01 NaN 2013-01-02 NaN 2013-01-03 1.0 2013-01-04 3.0 2013-01-05 5.0 2013-01-06 NaN Freq: D, dtype: float64 """ dataFrame2 = dataFrame1.sub(series2, axis = "index") print(dataFrame2) """ A B C D 2013-01-01 NaN NaN NaN NaN 2013-01-02 NaN NaN NaN NaN 2013-01-03 -0.501379 -0.658041 -0.828433 -1.567567 2013-01-04 -4.395337 -2.047519 -3.019128 -2.006114 2013-01-05 -3.406803 -4.196435 -5.327899 -3.937772 2013-01-06 NaN NaN NaN NaN """ |
▶ 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 명령을 실행했다.