■ DataFrame 클래스의 iloc 속성을 사용해 컬럼을 명시적으로 슬라이싱하는 방법을 보여준다.
▶ 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.418967 -0.833904 -1.134466 1.835324 2013-01-02 -0.868156 1.606709 -0.455127 -0.484539 2013-01-03 -1.772179 -0.491787 2.472023 0.353378 2013-01-04 -1.455474 0.054363 0.405077 -1.180987 2013-01-05 0.787927 -1.620420 1.945740 -0.311787 2013-01-06 -2.274122 1.185262 -0.923877 -0.539917 """ print() dataFrame2 = dataFrame1.iloc[:, 1:3] print(dataFrame2) """ B C 2013-01-01 -0.833904 -1.134466 2013-01-02 1.606709 -0.455127 2013-01-03 -0.491787 2.472023 2013-01-04 0.054363 0.405077 2013-01-05 -1.620420 1.945740 2013-01-06 1.185262 -0.923877 """ |
▶ 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 명령을 실행했다.