Language/Python
[Python] Pandas interpolate() : 보간법을 활용한 결측치 처리
나는 정은
2022. 11. 2. 14:43
✅ Interpolate 보간법을 통한 결측치 처리
DataFrame.interpolate(method='linear', *, axis=0, limit=None, inplace=False,
limit_direction=None, limit_area=None, downcast=None, **kwargs)
- 가장 간단한 선형보간법 (linear interpolation)이 default로 제공
- NaN값에 대해 가장 단순한 선형 회귀를 사용하여 데이터를 채워넣는다.
- 그 외에도 cubic, polynomial, 가장 가까운 값을 가져다 쓰는 nearest 등등을 사용한다.
- 시계열 데이터에서 가장 많이 사용 - 이전이나 이후값으로 대체
- NaN값에 대해 가장 단순한 선형 회귀를 사용하여 데이터를 채워넣는다.
- 특정 컬럼(column)만 보간법(interpolation)을 사용하고 싶다면, 데이터 프레임 전체 대신 특정 컬럼만 써주면 된다.
import pandas as pd
dates = ['10/23/2022','10/24/2022','10/25/2022',
'10/26/2022','10/27/2022']
dates = pd.to_datetime(dates)
df = pd.Series([62, np.nan, np.nan, 158, 189], index = dates)
df = df.interpolate()
2022-10-23 62.0
2022-10-24 94.0
2022-10-25 126.0
2022-10-26 158.0
2022-10-27 189.0
dtype: float64