■ DataFrame 클래스의 reindex 메소드에서 index/columns 인자를 사용해 인덱스/컬럼을 재구성하는 방법을 보여준다.
▶ 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 |
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.273991 0.146700 -0.962881 0.531161 2013-01-02 -0.018018 1.398183 -0.601010 -1.012397 2013-01-03 -0.692638 3.078527 0.164985 -0.842357 2013-01-04 -0.492567 -0.533595 -1.081510 -1.283123 2013-01-05 0.003707 -1.165857 1.957604 -0.203697 2013-01-06 0.474054 2.345456 0.426076 0.330338 """ print() dataFrame2 = dataFrame1.reindex(index = datetimeIndex[0:4], columns = list(dataFrame1.columns) + ["E"]) print(dataFrame2) """ A B C D E 2013-01-01 -0.273991 0.146700 -0.962881 0.531161 NaN 2013-01-02 -0.018018 1.398183 -0.601010 -1.012397 NaN 2013-01-03 -0.692638 3.078527 0.164985 -0.842357 NaN 2013-01-04 -0.492567 -0.533595 -1.081510 -1.283123 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 명령을 실행했다.