■ DataFrame 클래스의 to_numpy 메소드를 사용해 ndarray 객체를 구하는 방법을 보여준다.
▶ 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 dataFrame = pd.DataFrame( { "A" : 1.0, "B" : pd.Timestamp("20130102"), "C" : pd.Series(1, index = list(range(4)), dtype = "float32"), "D" : np.array([3] * 4, dtype = "int32"), "E" : pd.Categorical(["test", "train", "test", "train"]), "F" : "foo" } ) print(dataFrame) """ A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo """ ndarray1 = dataFrame.to_numpy() print(ndarray1) """ [[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo'] [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo'] [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo'] [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']] """ |
▶ 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 명령을 실행했다.