■ DataFrame 클래스의 groupby 메소드를 사용해 특정 컬럼들의 그룹별 평균값을 구하는 방법을 보여준다.
▶ 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 |
import pandas as pd sourceDataFrame = pd.read_csv("air_quality_no2_long.csv", parse_dates = ["date.utc"]) renameDataFrame = sourceDataFrame.rename(columns = {"date.utc" : "datetime"}) targetDataFrameGroupBy = renameDataFrame.groupby([renameDataFrame["datetime"].dt.weekday, "location"]) targetSeriesGroupBy = targetDataFrameGroupBy["value"] targetSeries = targetSeriesGroupBy.mean() print(targetSeries) """ datetime location 0 BETR801 27.875000 FR04014 24.856250 London Westminster 23.969697 1 BETR801 22.214286 FR04014 30.999359 London Westminster 24.885714 2 BETR801 21.125000 FR04014 29.165753 London Westminster 23.460432 3 BETR801 27.500000 FR04014 28.600690 London Westminster 24.780142 4 BETR801 28.400000 FR04014 31.617986 London Westminster 26.446809 5 BETR801 33.500000 FR04014 25.266154 London Westminster 24.977612 6 BETR801 21.896552 FR04014 23.274306 London Westminster 24.859155 Name: value, dtype: float64 """ |
※ renameDataFrame[“datetime”].dt.weekday 속성에서 0은 월요일, 6은 일요일이다.
▶ 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 명령을 실행했다.