■ DataFrame 클래스의 pivot 메소드에서 index/columns/values 인자를 사용해 LONG 포맷 데이터에서 WIDE 포맷 데이터를 구하는 방법을 보여준다.
▶ 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 |
import pandas as pd sourceDataFrame = pd.read_csv("air_quality_no2_long.csv", parse_dates = ["date.utc"]) renameDataFrame = sourceDataFrame.rename(columns = {"date.utc" : "datetime"}) pivotDataFrame = renameDataFrame.pivot(index = "datetime", columns = "location", values = "value") """ location BETR801 FR04014 London Westminster datetime 2019-05-07 01:00:00+00:00 50.5 25.0 23.0 2019-05-07 02:00:00+00:00 45.0 27.7 19.0 2019-05-07 03:00:00+00:00 NaN 50.4 19.0 2019-05-07 04:00:00+00:00 NaN 61.9 16.0 2019-05-07 05:00:00+00:00 NaN 72.4 NaN ... ... ... ... 2019-06-20 20:00:00+00:00 NaN 21.4 NaN 2019-06-20 21:00:00+00:00 NaN 24.9 NaN 2019-06-20 22:00:00+00:00 NaN 26.5 NaN 2019-06-20 23:00:00+00:00 NaN 21.8 NaN 2019-06-21 00:00:00+00:00 NaN 20.0 NaN [1033 rows x 3 columns] """ |
▶ 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 명령을 실행했다.