where 메소드의 통일감이 없는 유감스러운 알림

3443 단어 pandas파이썬numpy
numpy와 pandas where의 거동이 반하는 것처럼 느꼈기 때문에 공유합니다.

조건에 따라 값을 할당하는 메서드에는 wheremask가 있습니다.
메서드에 대한 자세한 내용은 여기에서 찾을 수 있습니다.
(언제나 신세를지고 있습니다 )

아래에 적당한 예를 내 보겠습니다.
import numpy as np
import pandas as py

test = np.array([1,2,3])

2200로 바꾸고 싶습니다.
np.where로 해 봅시다.
>>> pd.Series(np.where(test == 2, 200, test))
0      1
1    200
2      3
dtype: int64

원하는 결과를 얻을 수 있습니다.

그럼 pd.where 그럼?
>>> pd.Series(test).where(test == 2, 200)
0    200
1      2
2    200
dtype: int64

죄송합니다. 목적과는 반대의 결과가 되었다…
np.where 대신 pd.mask를 사용합시다.
>>> pd.Series(np.where(test == 2, 200, test))
0      1
1    200
2      3
dtype: int64
np.where 와의 거동을 맞추면 좋겠네요…

좋은 웹페이지 즐겨찾기