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]?

Asked By: Arvind Menon

||

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
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.