Home > OS >  Converting nan to NaN in python
Converting nan to NaN in python

Time:01-21

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]

Please advise as im a complete beginner to python. Thank you

CodePudding user response:

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
  • Related