■ DataFrame 클래스의 transform 메소드에서 lambda 함수를 사용해 컬럼 값을 변경하는 방법을 보여준다.
▶ 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 |
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 -0.348191 -1.264788 -0.162901 1.103283 2013-01-02 -0.524437 1.183184 -0.312923 2.145154 2013-01-03 0.230208 0.493941 1.820556 -1.041067 2013-01-04 0.399232 0.570309 -0.269344 -1.350358 2013-01-05 -0.785327 -1.059529 -0.642831 -1.075879 2013-01-06 -1.255896 0.641685 2.065610 0.004382 """ print() dataFrame2 = dataFrame1.transform(lambda x : x * 101.2) print(dataFrame2) """ A B C D 2013-01-01 -35.236936 -127.996495 -16.485594 111.652235 2013-01-02 -53.073035 119.738185 -31.667851 217.089579 2013-01-03 23.297028 49.986860 184.240241 -105.355954 2013-01-04 40.402308 57.715234 -27.257620 -136.656259 2013-01-05 -79.475111 -107.224318 -65.054500 -108.878999 2013-01-06 -127.096632 64.938487 209.039739 0.443483 """ |
▶ 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 명령을 실행했다.