Converting nan to NaN in python
Question:
I have a field [X]
in a df where it has missing values and I created a new field [Y]
based of the field [X]
using a sub string function.
df["Y"] = df["X"].astype(str).str[:4]
The field df[X]
has missing values identified as "NaN"
and the field df[Y]
has missing values identified as "nan"
Is it possible to convert the "nan"
values on the field df[Y]
to "NaN"
as same as on the field df[X]
?
Answers:
Use Series.mask
with Series.isna
:
df = pd.DataFrame({'X':['abndf', np.nan, 'ss', 'somestring']})
df["Y"] = df["X"].astype(str).str[:4].mask(df['X'].isna())
print (df)
X Y
0 abndf abnd
1 NaN NaN
2 ss ss
3 somestring some
I have a field [X]
in a df where it has missing values and I created a new field [Y]
based of the field [X]
using a sub string function.
df["Y"] = df["X"].astype(str).str[:4]
The field df[X]
has missing values identified as "NaN"
and the field df[Y]
has missing values identified as "nan"
Is it possible to convert the "nan"
values on the field df[Y]
to "NaN"
as same as on the field df[X]
?
Use Series.mask
with Series.isna
:
df = pd.DataFrame({'X':['abndf', np.nan, 'ss', 'somestring']})
df["Y"] = df["X"].astype(str).str[:4].mask(df['X'].isna())
print (df)
X Y
0 abndf abnd
1 NaN NaN
2 ss ss
3 somestring some