Suppose I have a Series with NaNs:
pd.Series([0, 1, None, 1])
I want to transform this to be equal to:
pd.Series([False, True, None, True])
You'd think x == 1
would suffice, but instead, this returns:
pd.Series([False, True, False, True])
where the null value has become False
. This is because np.nan == 1
returns False
, rather than None
or np.nan
as in R.
Is there a nice, vectorized way to get what I want?
Maybe map
can do it:
import pandas as pd
x = pd.Series([0, 1, None, 1])
print x.map({1: True, 0: False})
0 False
1 True
2 NaN
3 True
dtype: object
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments