I felt that the behavior of where
of numpy and pandas was contrary, so I will share it.
There are where
and mask
methods that assign values depending on the condition.
Details of the method can be found at here.
(I am always indebted to you: pray :)
Here's a good example.
import numpy as np
import pandas as py
test = np.array([1,2,3])
Suppose you want to replace this 2
with 200
.
Let's do it with np.where
.
>>> pd.Series(np.where(test == 2, 200, test))
0 1
1 200
2 3
dtype: int64
You will get the desired result.
So what about pd.where
?
>>> pd.Series(test).where(test == 2, 200)
0 200
1 2
2 200
dtype: int64
I'm sorry. The result was the opposite of what I was aiming for ...
Use pd.mask
instead of np.where
.
>>> pd.Series(np.where(test == 2, 200, test))
0 1
1 200
2 3
dtype: int64
I want you to match the behavior with np.where
...
Recommended Posts