Replace with default value in pandas

Viewed 87

I want to know if there is an easier way to replace a value with other when it is NaN, other than:

var = a if not pd.isnull(a) else b

The idea would be to avoid having to have to use the variable a

1 Answers

Simplier is use notna if working with scalars:

var = a if pd.notna(a) else b

In column use Series.notna:

df['col'] = df['col'].mask(df['col'].notna(), a)

df.loc[df['col'].notna(), 'col'] = a
Related