■ 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 |
import pandas as pd import numpy as np dataFrame1 = pd.DataFrame( { "x" : np.random.uniform(1.0, 168.0, 120), "y" : np.random.uniform(7.0, 334.0, 120), "z" : np.random.uniform(1.7, 20.7, 120), "month" : [5, 6, 7, 8] * 30, "week" : np.random.randint(1, 4, 120) } ) dataFrameGroupBy = dataFrame1.groupby(["month", "week"]) seriesGroupBy = dataFrameGroupBy["x"] dataFrame2 = seriesGroupBy.agg(["mean", "std"]) print(dataFrame2) """ mean std month week 5 1 88.947039 45.873949 2 84.626773 43.200584 3 88.982771 48.427208 6 1 86.000993 45.648100 2 88.483033 44.147915 3 104.315404 35.565770 7 1 78.417683 39.055948 2 83.794869 60.363769 3 74.237160 46.610472 8 1 67.600496 48.462412 2 88.180771 50.919670 3 85.207618 52.555796 """ |
▶ 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 명령을 실행했다.