Pandas 이동평균 구하기 rolling().mean()

단순 이동평균이란? 

이동평균(Moving Average)란 시간에 따른 데이터의 변화를 평균을 기준으로 부드럽게 만드는 방법으로 변화의 패턴(추세)을 파악하여 변동성을 줄이거나  노이즈 제거를 위해 사용한다. 

 

rolling().mean() 함수

Pandas의 rolling().mean()함수는 이동평균을 쉽게 구할 수 있게 돕는다.

rolling(window=).mean()

window크기를 정하면 window만큼의 데이터를 가져와서 평균(mean)을 구해준다. 이동평균을 구할 충분한 데이터가 없을 때 NaN이 나올 수 있다. 

 

rolling()함수는 일정 기간(Sliding Window) 동안의 집계함수(평균 mean, 합계 sum, 최소값 min, 최대값 max, 표준편차 std 등과 함께 사용할 수 있다. 

 

예시

 

참고

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html

 

pandas.DataFrame.rolling — pandas 2.2.3 documentation

If 0 or 'index', roll across the rows. If 1 or 'columns', roll across the columns. For Series this parameter is unused and defaults to 0. Deprecated since version 2.1.0: The axis keyword is deprecated. For axis=1, transpose the DataFrame first instead.

pandas.pydata.org

 

https://www.datamanim.com/dataset/99_pandas/pandasMain.html#grouping