David Davó
David Davó

Reputation: 690

Replace with default value in pandas

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

Upvotes: 2

Views: 191

Answers (1)

jezrael
jezrael

Reputation: 862851

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

Upvotes: 3

Related Questions