■ DataFrame 클래스의 loc 속성을 사용해 특정 컬럼의 데이터를 변경하는 방법을 보여준다.
▶ 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) dataFrame = pd.DataFrame(np.random.randn(6, 4), index = datetimeIndex, columns = list("ABCD")) print(dataFrame) """ A B C D 2013-01-01 -1.024513 2.729313 0.866363 0.781470 2013-01-02 -0.594614 -0.238171 -0.041801 -0.337448 2013-01-03 -0.168112 -0.125254 0.037904 0.376932 2013-01-04 0.462503 0.292850 -1.011982 1.821977 2013-01-05 -1.207941 -0.644128 -2.173165 -0.039098 2013-01-06 0.684733 0.323357 1.083395 0.600062 """ print() dataFrame.loc[:, "D"] = np.array([5] * len(dataFrame)) print(dataFrame) """ A B C D 2013-01-01 -1.024513 2.729313 0.866363 5.0 2013-01-02 -0.594614 -0.238171 -0.041801 5.0 2013-01-03 -0.168112 -0.125254 0.037904 5.0 2013-01-04 0.462503 0.292850 -1.011982 5.0 2013-01-05 -1.207941 -0.644128 -2.173165 5.0 2013-01-06 0.684733 0.323357 1.083395 5.0 """ |
▶ 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 명령을 실행했다.