■ Series 클래스의 dt 속성을 사용해 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 |
import pandas as pd import matplotlib.pyplot as plt airQualityNO2DataFrame = pd.read_csv("air_quality_no2_long.csv", parse_dates = ["date.utc"]) renameAirQualityNO2DataFrame = airQualityNO2DataFrame.rename(columns = {"date.utc" : "datetime"}) datetimeSeries = renameAirQualityNO2DataFrame["datetime"] renameAirQualityNO2DataFrame["month"] = datetimeSeries.dt.month print(renameAirQualityNO2DataFrame) """ city country datetime location parameter value unit month 0 Paris FR 2019-06-21 00:00:00+00:00 FR04014 no2 20.0 µg/m³ 6 1 Paris FR 2019-06-20 23:00:00+00:00 FR04014 no2 21.8 µg/m³ 6 2 Paris FR 2019-06-20 22:00:00+00:00 FR04014 no2 26.5 µg/m³ 6 3 Paris FR 2019-06-20 21:00:00+00:00 FR04014 no2 24.9 µg/m³ 6 4 Paris FR 2019-06-20 20:00:00+00:00 FR04014 no2 21.4 µg/m³ 6 ... ... ... ... ... ... ... ... ... 2063 London GB 2019-05-07 06:00:00+00:00 London Westminster no2 26.0 µg/m³ 5 2064 London GB 2019-05-07 04:00:00+00:00 London Westminster no2 16.0 µg/m³ 5 2065 London GB 2019-05-07 03:00:00+00:00 London Westminster no2 19.0 µg/m³ 5 2066 London GB 2019-05-07 02:00:00+00:00 London Westminster no2 19.0 µg/m³ 5 2067 London GB 2019-05-07 01:00:00+00:00 London Westminster no2 23.0 µg/m³ 5 [2068 rows x 8 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 명령을 실행했다.