파이썬(Python)&판다스(Pandas)&Polars
[Pandas] 빈 값(NaN, None)에 원하는 값 채우기(pd.where)
송채채
2024. 1. 3. 13:37
pd.where(조건문, 실행문) 형태로 빈 값에 원하는 값을 채울 수 있다.
- np.nan와 None을 통일시키고 싶을 때 사용할 수 있음
df = df.where(pd.notnull(df), None)
- where를 사용해서 다양한 조건문으로 데이터를 조작할 수 있음
>>> s = pd.Series(range(5))
>>> s.where(s > 0)
0 NaN
1 1.0
2 2.0
3 3.0
4 4.0
dtype: float64
>>> s.mask(s > 0)
0 0.0
1 NaN
2 NaN
3 NaN
4 NaN
dtype: float64
아래 공식문서를 참고
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 2.1.4 documentation
Where cond is True, keep the original value. Where False, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Serie
pandas.pydata.org
반응형