■ merge 함수의 how/left_on/right_on 인자를 사용해 데이터를 조인하는 방법을 보여준다.
▶ 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 airQualityParameterDataFrame = pd.read_csv("air_quality_parameters.csv") airQualityNO2DataFrame = pd.read_csv("air_quality_no2_long.csv" , parse_dates = True)[["date.utc", "location", "parameter", "value"]] AirQualityPM25DataFrame = pd.read_csv("air_quality_pm25_long.csv", parse_dates = True)[["date.utc", "location", "parameter", "value"]] airQualityDataFrame = pd.concat([AirQualityPM25DataFrame, airQualityNO2DataFrame], axis = 0).sort_values("date.utc") targetDataFrame = pd.merge(airQualityDataFrame, airQualityParameterDataFrame, how = "left", left_on = "parameter", right_on = "id") print(targetDataFrame) """ date.utc location parameter value id description name 0 2019-05-07 01:00:00+00:00 London Westminster no2 23.0 no2 Nitrogen Dioxide NO2 1 2019-05-07 01:00:00+00:00 FR04014 no2 25.0 no2 Nitrogen Dioxide NO2 2 2019-05-07 01:00:00+00:00 BETR801 pm25 12.5 pm25 Particulate matter less than 2.5 micrometers i... PM2.5 3 2019-05-07 01:00:00+00:00 BETR801 no2 50.5 no2 Nitrogen Dioxide NO2 4 2019-05-07 01:00:00+00:00 London Westminster pm25 8.0 pm25 Particulate matter less than 2.5 micrometers i... PM2.5 ... ... ... ... ... ... ... ... 3173 2019-06-20 22:00:00+00:00 FR04014 no2 26.5 no2 Nitrogen Dioxide NO2 3174 2019-06-20 23:00:00+00:00 London Westminster pm25 7.0 pm25 Particulate matter less than 2.5 micrometers i... PM2.5 3175 2019-06-20 23:00:00+00:00 FR04014 no2 21.8 no2 Nitrogen Dioxide NO2 3176 2019-06-21 00:00:00+00:00 London Westminster pm25 7.0 pm25 Particulate matter less than 2.5 micrometers i... PM2.5 3177 2019-06-21 00:00:00+00:00 FR04014 no2 20.0 no2 Nitrogen Dioxide NO2 [3178 rows x 7 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 명령을 실행했다.
air_quality_parameters.csv
air_quality_no2_long.csv
air_quality_pm25_long.csv