■ DataFrame 클래스를 사용해 데이터 변형하는 방법을 보여준다.
▶ 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import pandas as pd import numpy as np list1 = list(range(1, 24)) + [np.nan] """ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, nan] """ ndarray = np.array(list1).reshape(2, 3, 4) print(ndarray) """ [[[ 1. 2. 3. 4.] [ 5. 6. 7. 8.] [ 9. 10. 11. 12.]] [[13. 14. 15. 16.] [17. 18. 19. 20.] [21. 22. 23. nan]]] """ print() dataFrame = pd.DataFrame([tuple(list(x) + [val]) for x, val in np.ndenumerate(ndarray)]) print(dataFrame) """ 0 1 2 3 0 0 0 0 1.0 1 0 0 1 2.0 2 0 0 2 3.0 3 0 0 3 4.0 4 0 1 0 5.0 5 0 1 1 6.0 6 0 1 2 7.0 7 0 1 3 8.0 8 0 2 0 9.0 9 0 2 1 10.0 10 0 2 2 11.0 11 0 2 3 12.0 12 1 0 0 13.0 13 1 0 1 14.0 14 1 0 2 15.0 15 1 0 3 16.0 16 1 1 0 17.0 17 1 1 1 18.0 18 1 1 2 19.0 19 1 1 3 20.0 20 1 2 0 21.0 21 1 2 1 22.0 22 1 2 2 23.0 23 1 2 3 Na """ |
▶ 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 명령을 실행했다.